Understanding the problem:
I have been doing a lot of work on developing custom site definitions for SharePoint applications. SharePoint allows you to create a custom site definitions as well as modify and customize the out-of-the-box site definitions. I have worked on both the approaches. But there is a very serious issue with the latter one – modifying and customizing out-of-the-box site definitions.
When you modify and customize an out-of-the-box site definition, you basically modify its onet.xml, master pages, page layouts, etc and you may also add new master pages, page layouts, web pages, etc. But if in future, you will install any service pack, hot fix, or upgrade to new version, most likely all your changes and customizations will be overwritten.
I did a trick to avoid such situations. I made a copy of the out-of-the-box site definition, did all the customizations in the copied one. Then I hided the original site definition from the users. So users can see only the customized site definitions and if any service pack or hot fix will be installed, it will overwrite only the original site definition and the customized site definitions will be untouched.
Solution provided by SharePoint - Feature Stapling:
But SharePoint also provides a solution to handle such kind of situations, and the solution is Feature Stapling. Feature Stapling allows you to “staple” a Feature to both out-of-the-box and custom site definitions without modifying them in any way. This means you can add your Features to all sites created using that site definition. So if you want to do any modifications or customizations in out-of-the-box site definitions; develop them all as Features and staple them with the respective site definition.
How to staple a Feature to a Site Definition:
To staple a Feature to a site definition, you actually need to create another Feature that will do the stapling, and this feature is called as Stapler Feature. A feature which is going to be stapled/associated with a site definition is called as Staplee Feature. Below is an example of a staple feature to staple a Multilanguage feature to the STS, and SPS site definitions:
Feature.xml file (for stapler feature):
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="82E2EA42-39E2-4B27-8631-ED54C1CFC491"
Title="$Resources:MultiLangStaplingFeatureName"
Description="$Resources:MultiLangstaplingFeatureDescription"
Version="12.0.0.0"
Scope="Farm"
xmlns="http://schemas.microsoft.com/sharepoint/"
DefaultResourceFile="_Res">
<ElementManifests>
<ElementManifest Location="Elements.xml"/>
</ElementManifests>
</Feature>
Elements.xml file (for stapler feature):
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation Id="29D85C25-170C-4df9-A641-12DB0B9D4130" TemplateName="STS#0" />
<FeatureSiteTemplateAssociation Id="29D85C25-170C-4df9-A641-12DB0B9D4130" TemplateName="STS#1" />
<FeatureSiteTemplateAssociation Id="29D85C25-170C-4df9-A641-12DB0B9D4130" TemplateName="SPS#0" />
</Elements>
In the above Elements.xml file code, you can see the FeatureSiteTemplateAssociation element which basically defines the associations between Features and site definitions. In the above example, the stapler feature “staples” the staple Feature with the ID “29D85C25-170C-4df9-A641-12DB0B9D4130” to the STS#0, STS#1, and SPS#0 site definitions.
The scope of the stapler feature is always Farm. If the stapler feature is deactivated, the existing sites will still have the Multilanguage feature activated but no new sites will have the Multilanguage feature activated.
Feature stapling is very powerful as it allows you to add functionality to site definitions without having to modify the site definitions themselves.
One more thing, if you want to staple your Feature to all site definitions then you can staple it to the GLOBAL site definition and it will be added to all sites that are created.