Feature Stapling

Feature stapling is a nice new addition to SharePoint that allows you to tie in an existing Feature to a site definition. It's actually pretty simple to set up. There are 2 ways to staple a Feature to your site definition. One way is to edit the onet.xml file and the other way is to create another feature that ties your Feature to the target site definition.

NOTE: A stapled Feature is automatically activated when your site definition is used.

If you want to staple your Feature directly to a site definition, all you have to do is make a single entry to the onet.xml file for your site definition. Under the Configuration element, you'll see SiteFeatures and WebFeatures. As you can see in the image below, my site definition has 2 Features under each section. You'll use SiteFeatures if you want your feature activated when your site definition is used to create a new site collection. WebFeatures is used when you want the Feature to activate on an individual web that is created using your site definition. The ID in the Feature element is the guid given to your Feature.




The other way to staple a Feature is to create another Feature that does the stapling. If you look at the image below, the feature.xml file is constructed like a typical feature.xml file.





All of the work happens in the Element.xml file. As you can see, there is only one entry in the Elements node. FeatureSiteTemplateAssociate takes two attributes. Id, and TemplateName. Id is the guid assigned to the Feature that you want to staple. TemplateName is a combination of two values found in your webTemp*.xml file(s); your definition name and the configuration id. You can get the first half from the Template element's Name attribute. If you look at my example below, my Feature will be stapled to a standard "Team Site".




The webtemp file will be located in the TEMPLATE\12\xml directory and will look similar to the following image. I've highlighted the values needed to staple my Feature to the Team Site. Make sure to put the # symbol in between the two values in you element file.




That's it. Just remember that your Feature will automatically activate when the site definition is used. Any previously created site definitions will need your Feature activated manually.

Labels: