Feature Stapling

In this article I will describe that What is feature stapling and how to implement in SharePoint.

Introduction:-

Causes the attachment of a Feature to all new instances of sites that use a given site definition, without modifying the site definition or creating code routines to activate the Feature on each site. Also known as a feature site template association.

Requirement:-

Most often; we have some requirement (for example branding of site which includes master page, layout, themes etc.) which need to execute when we create site from specific site definition.

To handle this situation we create mediator (called feature stapling) which associate site definition and our feature. So that whenever admin create site using specific site definition, it activate our feature also.

Technical Detail:-

I will take example of custom master page creation. Assume I have master page and whenever I create a site from team site definition, I wish to execute my feature which upload master page and make as default master page. Follow below steps:

Create 1st project:-

Create a project where you have master page and feature receiver class which set this master page as default master page. (This part I am not going to explain in detail since this article is to explain about stapling). But you can see the structure of project and feature.xml as mentioned below:-

                         feature stapling

                           feature stapling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Feature  Id="A5AB8768-4458-467c-BEAD-31220B54E48D"
          Title="Avinash Collaboration Branding Base"
          Description="This is the master page for collaboration sites."
          Version="12.0.0.0"
          Hidden="TRUE"
          Scope="Web"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/"
          ReceiverAssembly="Avinash.Features.Collaboration.BrandingBase, 
                      Version=1.0.0.0, Culture=neutral, PublicKeyToken=e7e8bc17ad048a66"
          ReceiverClass="Avinash.Features.Collaboration.BrandingBase.FeatureReceiver">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>


Create 2nd Project:-

Now you create another project for feature stapling (Note a can this stapling feature in same project which you created 1st for master page).

Add 2 below files

feature.xml

here Mention the path of elements.xml file.
Point to be noted: feature id should be different for both 1st and 2nd project.

1
2
3
4
5
6
7
8
9
10
11
12
<Feature  Id="6F23C6FD-4E2A-4066-9C03-DC3266B9ED91"
          Title="Collaboration BrandingBase Stapling"
          Description="Staples the Branding feature to all collaboration site definitions."
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="WebApplication"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>


elements.xml

Here i have specified id as same as feature of 1st project. And associated to various site template. you can associate as per your requirement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="STS#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="STS#1" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="STS#2" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="WIKI#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="BLOG#0" />
 
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="MPS#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="MPS#1" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="MPS#2" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="MPS#3" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="MPS#4" />
 
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="SRCHCENTERLITE#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="SRCHCENTERLITE#1" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="BDR#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="OFFILE#0" />
  <FeatureSiteTemplateAssociation Id="A5AB8768-4458-467c-BEAD-31220B54E48D" TemplateName="SPSREPORTCENTER#0" />
</Elements>

Structure of stapling feature will look like below image

                                  feature stapling

Now you are all set to deploy. Create WSP, deploy and test.

Reference:-

http://msdn.microsoft.com/en-us/library/ff648422.aspx
http://msdn.microsoft.com/en-us/library/bb861862%28v=office.12%29.aspx

Hope it helps.
Thanks!
Avinash

calendarMarch 15, 2012 · cardInfoyen · comments2 Comments
tagTags: , , , ,  · Posted in: Branding, MOSS, SharePoint

2 Responses

  1. Yetanotheruser - May 29, 2012

    Hi Avinash, thanks for terse example. Is there any relationship between feature stapling and unghosting of the site’s pages? Options for applying branding customization to a SP portal typically start with SP Designer, but this is intriguing, if an alternate possibility. Thanks.

  2. Infoyen - May 31, 2012

    It’s good to see that some one like the post.
    Answer to your questions:-

    – As I explained in articles feature stapling is just a process by which you do something while creating site or sub sites. That means you can upload master page, layouts, css, js and do some action on feature receiver event to set your custom master and theme on site creation.

    – About customization:- you need to customize master page and layouts at your dev. Once its done. Copy all these files and keep in your WSP solution. And in your site delete all custom files. Then deploy files using WSP solution.
    If you follow this process that means sharepoint treats that you never customized any file and therefore your feature update master page, layouts etc..

    Moral: modify masterpage, layouts etc using SPD (just to preview your output) but never checkin. Deploy using WSP only.

    Hope it helps.
    Avinash

Leave a Reply

Spam Protection: , required

myworldmaps infoyen