Saturday, May 23, 2009

Feature Stapling in Sharepoint

           Before you pull your extra time to read this article, let us make sure you are reading the right article for your problem.This article address the best approach of customizing the site defintion/templates and gives an insight how sharepoint defines,structures site templates.

CaseStudy: I've a requirement to create some custom sharepoint site definition,just to add some custom links/look to existing templates.So we have two approaches that can be followed here:
1. Edit onet.xml(present in 12 hive folder) of the base site defintion to add customizations - Read here
2. Use feature stapling concept to do it - Best approach

Lets see how the best approach(feature stapling) works, step by step;
feature stapling does need 2 features to accomplish our work.
One feature, staplee that really adds the customizations to our OOTB or custom site defintion
and the other feature, stapler that binds associates the above feature with the site definiton. Bascially we dont need not go into risk of editing onet.xml of site definiton neither stapling does. Thats simple and clean. Did you get a doubt how/when it adds cusotmizations? When ever a site is created based on this site template then the customizations defined in the staplee feature get added to the site .

Create a stapler :
Stapler will have 2 xml files, feature.xml and a manifest xml(here elements.xml)

<?xml version="1.0" encoding="utf-8" ?>
<Feature  Id="82E2EA42-39E2-4B27-8631-ED54C1CFC492"
        <ElementManifest Location="elements.xml"/>

Elements.xml for the above stapler feature is as described as below, where the site template name is associated with the staplee feature(which adds customizations)

<Elements xmlns="">
   <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="BDR#0" />
   <FeatureSiteTemplateAssociation Id="29D85C25-170C-4df9-A641-12DB0B9D4130" 
TemplateName="SPS#0" />

Here the ID represent the staplee feature where customizations are defined or coded.
TemplateName is the OOTB template name along with config id(optional) or the cusotm name you have given. Say if you want the feature to be associated with all the site templates(different config id's) that are defined under a single template name then just name would be enough with no id.To apply the feature to entite site templates in the farm/sharepoint set TemplateName = Global#0
Click here for all the OOTB site template ids.
Thats great,Now all your customizations are added to the site definitions you want.

Possible scopes for a feature stapler is either Farm or WebApplication or SiteCollection


No comments:

Post a Comment