Feature Stapling in SharePoint 2010

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
  • Workflows
  • 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.

Code Behind

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″ ?>
<Feature
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=”1.0.0.0” />

Elements.xml

<?xml
version=1.0
encoding=utf-8 ?>

<Elements
xmlns=http://schemas.microsoft.com/sharepoint/>

<FeatureSiteTemplateAssociation
Id=b8f334a0-1ffe-420d-b6e2-54a9faa6ba95
TemplateName=STS#0 />

<FeatureSiteTemplateAssociation
Id=07cb8bd4-7004-4021-8355-48d17de287e5
TemplateName=STS#0 />

<FeatureSiteTemplateAssociation
Id=90C924A7-32F7-4C19-BD4C-AF49EF0563A9
TemplateName=STS#0 />

</Elements>

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.

 

 

 

 

Advertisements

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

%d bloggers like this: