Feature stapling allows you to associate a set of SharePoint features with any site definition. Therefore, whenever any site collection is provisioned from that site definition, the features associated with it gets activated automatically.
Lets go ahead and see how to apply feature stapling. For this demo, I would staple these 2 OOB sharepoint features with Team Site (Site Definition)-
|SharePoint server Publishing infrastructure||F6924D36-2FA8-4f0b-B16D-06B7250180FA|
1) In your sharepoint solution project, add an ‘Empty Element’ type item. In it’s elements.xml, add the following code.
<!–Three-state Workflow Feature –>
<FeatureSiteTemplateAssociation Id=“FDE5D850-671E-4143-950A-87B473922DC7” TemplateName=“STS#0” />
<!–Sharepoint server publishing infrastructure –>
<FeatureSiteTemplateAssociation Id=“F6924D36-2FA8-4f0b-B16D-06B7250180FA” TemplateName=“STS#0” />
STS#0 is the template name for Team Site site definition.
2) Set the scope of this stapler feature to WebApplication. You can also set it’s scope to FARM and SITE (sitecollection) but not WEB.
Build and deploy your sharepoint solution. Now, you will find that whenever you create any sitecollection from ‘Team Site’ site definition, these two SP features will be automatically activated.
Similarly, you can also associate your custom SharePoint features with any site definition by performing steps similar to shown above.
Some important notes about scope and sequence that you need to be aware of while implementing feature stapling.
1) If Stapler feature is ‘Farm’ scoped then activation order will be –
– Features from site definition will be activated first.
– Followed by features without activation dependencies from stapler.
– then, features with activation dependencies from stapler.
2) If stapler feature is ‘WebApplication’ scoped then
– features gets activated in random order.
In order to enforce a fixed sequence, workaround shown below can be considered –
1) Create a new feature (Assume its GUID is ‘89635fbe-af7c-443e-bb5c-610b155bef2e’) whos only purpose is to activate other features.
Add a feature event receiver and then write code similar to below –
Guid featureOneGuid = new Guid(“SomeFeature1GUID”); //site scoped
Guid featureTwoGuid = new Guid(“SomeFeature2GUID”); //web scoped
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
2) Then, in the stapler feature just associate the above feature with the site definition such as –
<!–Main Feature –>
<FeatureSiteTemplateAssociation Id=”89635fbe-af7c-443e-bb5c-610b155bef2e” TemplateName=”STS#0″ />