Site stapling was introduced in WSS 3.0/MOSS in order to automatically associate and activate a feature with a site template without having to modify its definition.
More information can be found in the SharePoint SDK here.
For instance, let’s say you want all SharePoint site created from the blank template to include a logging list, either you modify the blank site template definition or you staple a custom feature with the team site template I was surprised to see that not many SharePoint devs know this functionality.
I will quickly explain the concept and mention a bug that I’ve just discovered.
For instance, let’s create a new SharePoint 2010 project with Visual Studio 2010
Select the farm option.
Add a new SharePoint project Item from the List Definition template, name it LoggingList :
Your list definition must be based on the custom list template and check the option Add a list instance for this list definition :
This is the feature we want to staple.Now we need to create the stapling feature.
Create a new (empty) feature in Visual Studio and name it DemoStaplingFeature and set its scope to Farm in the feature Designer :
Add an Empty element SharePoint project item:
In the newly created Elements.xml file, add the FeatureTemplateAssociation tag and associate the first feature id with STS#0, which is the Team site template.
Make sure that only the StaplingElement SharePoint project item is included in your Demostapling feature and that this element is not part of the other feature.
Deploy your solution, there is no need to activate your DemoStapling feature because like any Farm feature it is automatically activated in the whole farm.
If you create a team site, the logging list will be created automatically.
BUG : stapling to the GLOBAL TEMPLATE.
In theory (and also according to the SDK I mentioned above), if you want you feature to be stapled to EVERY web site template of your farm, you just have to specify GLOBAL instead of STS#0. The bad new is that it doesn’t work, you still have to have one FeatureSiteTemplateAssociation for every site definition/template, which is every annoying: indeed if a new site definition is deployed , we have to update & redeploy our farm feature.
What is very strange is that if we take a look at the code provided with SharePoint 2010 in the TaxonomyFeatureStappler for instance
we can see that Microsoft still staples the feature to GLOBAL and to other features; something must be wrong (see the picture below):
Anyway, feature stapling is interesting, but don’t over abuse them.
Feature-stapling won’t affect any sites which have already been created from a site definition, only subsequent sites.