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″ />
</Elements>

This is explained below

<FeatureSiteTemplateAssociation
Id = “Text”
TemplateName = “Text”>
<Property
Key= “Text”
Value = “Text”>
</Property>
</FeatureSiteTemplateAssociation>

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

Advertisements

One thought on “Feature Stapling…

  1. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s