Feature stapling is a type of process where you attach (staple) a SharePoint feature to a site definition without modifying the site definition files.
Remember there are two site types: WebTemplates and Site Definitions.
- Site definitions consist of XML and ASPX files on the web front end server inside the 14 hive directory.
- Web templates on the other hand are stored in the database. They are created by using an existing site with or without content. This provides a way to reuse sites that are customized. Since custom web templates are based on other sites, the first site deployed must be configured within a site definition.
Be sure to review this link to understand how Microsoft explains site types in detail.
Remember, a SharePoint feature is a package of elements that can be deployed to a site allowing for customization. Examples of elements that are packaged as a feature include:
- Web parts
- List templates
- Menu items
What is Feature Stapling?
The technique of feature stapling allows you to attach or ‘staple’ a SharePoint feature to a site definition without making any changes to the original site definition. This process offers the ability to add features and customize all sites that are built using that site definition.
In 2010, feature stapling has 2 features. A ‘stapler’ feature staples another feature to a site definition and a ‘staplee’ feature which is a feature that will be stapled.
One example of feature stapling would be applying a custom theme to all Publishing site definitions. By creating a feature that will be applied to the custom theme to a site, it can be associated with the Publishing site’s site definition. Another example would be activating a custom list to all Team sites. Therefore, when a team site is created, a feature is activated and creates a custom list.
The feature.xml and elements.xml files are modified to create a feature that is stapled to all site definitions. Take a look at the feature.xml below which contains a feature event receiver scoped at the farm level.
<?xml version=”1.0″ encoding=”utf-8″ ?>
xmlns=”http://schemas.microsoft.com/sharepoint/“ Id=”C384D136-79A8-48BD-AF73-C630547F4D8E“ Title=”MyFirstStapler“ Description=”This feature staples the other feature to the Team Site definition.“ Scope=”Farm“ Hidden=”False“ Version=”126.96.36.199” />
The FeatureSiteTemplateAssociationID is a GUID that maps features to the site definitions. Feature-stapling won’t affect any sites which have already been created from a site definition, only subsequent sites. The only way to do this is to activate your features individually on the site.The feature stapler is deployed at the web application level. Once a team site is created, the associated ‘stapled’ features become activated.