SharePoint Feature Stapling

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)-

Feature Name FeatureID
SharePoint server Publishing infrastructure F6924D36-2FA8-4f0b-B16D-06B7250180FA
Three-State workflow FDE5D850-671E-4143-950A-87B473922DC7

 1) In your sharepoint solution project, add an ‘Empty Element’ type item. In it’s elements.xml, add the following code.

<?xml version=1.0encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

 <!–Three-state Workflow Feature –>

<FeatureSiteTemplateAssociation Id=FDE5D850-671E-4143-950A-87B473922DC7TemplateName=STS#0/>

<!–Sharepoint server publishing infrastructure –>

<FeatureSiteTemplateAssociation Id=F6924D36-2FA8-4f0b-B16D-06B7250180FATemplateName=STS#0/>

 </Elements>

 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.

Example –

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;

site.Features.Add(featureOneGuid);

web.Features.Add(featureTwoGuid);

 2) Then, in the stapler feature just associate the above feature with the site definition such as –

<?xml version=”1.0″encoding=”utf-8″?>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>

 <!–Main Feature –>

<FeatureSiteTemplateAssociation Id=”89635fbe-af7c-443e-bb5c-610b155bef2e”  TemplateName=”STS#0″ />

 </Elements>

Leave a Comment

Your email address will not be published. Required fields are marked *