Feature Stapling…

What is feature stapling?

While doing SharePoint development, almost all cases I used to create my own Site Definitions. So what ever features to be activated, page instance to be created, was very easily defined in the onet.xml. But once I had a requirement to use a OOB site definition for one of my project. But when I create a site using the given OOB template I had to activate some OOB features also. To achieve that requirement I had to copy the existing site definition and create my own one, with the above feature activated by default. But that was not the best solution for the problem. Actually it was a worse solution ever Smile.

So while searching through internet, I found there is a cool built-in feature in SharePoint to do this. It’s called almighty “Feature Stapling”.

How it works?

You will need one more feature as a staple feature. In that feature you specify what additional feature or features should be activated when a site created using a specified site template.

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<FeatureSiteTemplateAssociation Id=”94C94CA6-B32F-4da9-A9E3-1F3D343D7ECB” TemplateName=”MySiteDefName#0″ />

This is explained below

Id = “Text”
TemplateName = “Text”>
Key= “Text”
Value = “Text”>

Id and the TemplateName are required fields. Id is the feature id that should get activated,

and the TemplateName is the subjective template that should activate this feature when a site created based on the template.

What are the possible values for TempalteName?

TemplateName can be any valid site definition name with a proper configuration value. That can be a OOB template or a custom template that you have created.

Is there a way to activate the feature for all existing Templates?

Yes. you can use the TempalteName=”Global” so this feature will be activated for all sites created using any site template.

What is the Feature Activation scope for the stapling feature?

You can only scope to “Farm” or “WebApllication” level.

Will my feature stapling will affect existing(already created) sites also?

No. It will not affect any of the already existing sites. If you need to activate the feature for existing site you will need to activate the feature manually on each site.

Will my feature stapling will affect any new site templates added to the farm in future?

Yes. All new site templates, added will get affected (depending on the scope of the activated stapled feature)

Hope this will help some one Smile

One comment

  • Yana Freedman says:

    An issue I came across while developing using stapled Features: Stapled Features, just like regular Features, are processed before elements from the site definition are provisioned. Because of this, accessing any site-specific pages in the FeatureActivated override method for the feature event receiver will fail. The solution it to provision the necessary pages explicitly. In this example, default.aspx is provisioned:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    SPWeb web = (SPWeb)properties.Feature.Parent;
    SPFile spFile = web.GetFile(“default.aspx”);

    if (spFile.Exists)
    // Do something with the page

Leave a Reply

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