Print Version Email Page Add to Favorites Comments Alert Me Add to My Links

Friday, August 22, 2008

MOSS 2007 : Feature Stapling

Introduction
Recently, I came across a situation, where-in I need to apply our custom theme to sharepoint site upon creation, i.e. setting our custom theme as a default one.
We generally have to go for customizing the out of box templates. In such a case, modifying the 12 hive folder is one option, but isn’t recommended as it will render the site unsupported in case of an upgrade.

In this post, I will be demonstrating how to attach a custom feature with a site template. Feature Stapling allows to staple a Feature to a site definition without modifying it in any way. To use feature-stapling, you first need the feature(s) you wish to link to the site definition - these should be created in the normal way. Then you should create a 2nd feature which does the actual stapling.

Step 1 - Create a Sample Feature
Create a sample feature, which will be activated every time a site is created with a specified template.
  • Create a Class Library project and inherit SPFeatureReceiver class
  • Override FeatureActivated() method.
  • Strongly name the assembly.
  • Add assembly to GAC.


  • Create a folder called ACMEThemeFeature inside FEATURES folder. (%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES)
  • Create a feature.xml file inside ACMEThemeFeature folder.


  • Run the following command to install this feature:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o installfeature -name ACMEThemeFeature

Step 2 - Associate a Template with Feature
After creating and installing a feature, we need to associate a template with this feature, for that purpose we will again create a feature but with scope as FARM (This is called a stapler feature).
  • Create a folder called ACMEThemeStapler inside FEATURES folder.
  • Create Feature.xml file inside ACMEThemeStapler folder.


  • Create Element.xml inside ACMEThemeStapler folder.


  • FeatureSiteTemplateAssociation ID inside Element.xml is id of the ACMEThemeFeature feature.
  • TemplateName is the teamplet name, you will get a list of TemplateName from Roadburn’s SharePoint 2007 blog.
  • Install ACMEThemeStapler feature using below command from command prompt:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o installfeature -name ACMEThemeStapler

Now, whenever a new site is created, ACMEThemeFeature feature will be activated and the will apply the specified theme to site.

World Clock