In this article we can explore how to do Feature Stapling.
What is Site Definition?
Site Definition consists of XML and ASPX files residing in the web server file system; they allow creation of site through pre-configured settings.
For example, you can create a Department Site Definition with:
1. One document library named Documents
2. One list named Contacts
3. One web part named Announcements
Later, this site definition can be used to create multiple department sites like Accounting Department, HR Department. The site instances will have the pre-configured uniform assets & behavior.
We have a Site Definition with Contacts list.
Customers created site instances out of this Site Definition.
We need to add a Tasks list to the Site Definition.
How the new Tasks lists get integrated to existing site instances?
Solution 1: Update Site Definition
In this solution, the site definition is updated with the new Tasks list.
But, there are chances it will break the existing site instances. Hence not recommended.
Solution 2: Feature Stapling
Create a new feature & add the Tasks list to the feature.
Staple the feature along with the previous Site Definition.
In this approach, whenever new site instances are created from our Site Definition, the new Tasks list get attached to it, through feature stapling.
But, existing site instances won't get updated. We need to do the following:
1. Manual Creation of new list in existing site instances
2. Creating new list through code in the Feature Activation event on existing site instances
Following is the POC on Feature Stapling
Feature stapling is a technique that allows for a feature to be stapled to a site definition. This allows the feature to be automatically activated on the Site Instance creation from Site Definition. We can also Feature Staple along with SharePoint Out-of-the-box site definitions.
Feature Stapling consists of 2 Features:
1. Staplee Feature The feature which we attach to the Site Definition.
2. Stapler Feature The feature that will actually do the Stapling.
Please follow the steps below to achieve Feature Stapling.
We are going to attach a My Tasks list for all future Team Site instances.
Step 1 Create Project
Choose the Farm Solution option.
Step 2: Add Staplee Feature
Here we are adding the feature which will create the My Tasks list.
Step 3 Add Event Receiver
Right click on the feature & choose Add Event Receiver
In the appearing code file, uncomment the Feature Activated event, add the following code.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPWeb web = (properties.Feature.Parent as SPWeb);
Guid guid = web.Lists.Add("MY TASKS", "Created through Feature Stapling", SPListTemplateType.Tasks);
SPList list = web.Lists[guid];
list.OnQuickLaunch = true;
The code creates the MY TASKS list & add to the quick launch.
Step 4 Add Stapler Feature
This feature will associate the above feature with the site definition.
Please note that we choose the scope as Web Application.
Step 5 Add Empty Element
Add an empty element to the project & rename it as FeatureStaplerElement.
Replace the content of XML file with the following.
<?xml version="1.0? encoding="utf-8??>
<FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#0? />
<FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#1? />
<FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#2? />
The code basically adds the MY TASKS feature to the site definitions of templates Team Site, Blank Site etc. using the IDs respectively.
(In your case please use your Site Definition ID)
Note The feature id can be received using the Properties Window.
Our My Tasks Feature looks like this:
Our Stapler Feature looks like this.
Step 6 Deploy Solution
Right click on the solution & choose Deploy option.
Step 7 Testing the Solution
Go to Central Administration & Create a new site collection of type Team Site.
You will be seeing the new site collection with our MY TASKS list as shown below.
This concludes our Feature Stapling demonstration.
In this article we have explored Feature Stapling scenario.