PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Introduction in Feature Stapling

Posted by PANVEGA on January 7, 2009

The focus of this article is going to be on a concept called Feature Stapling. Most, if not all, SharePoint developers know that Microsoft frowns on modifications to the “Microsoft” owned files that support SharePoint. If you modify these files you run the risk of your changes being broken with the installation of a service pack or hot fix.

Feature Stapling allows you to create a feature and then associate it with any site definition without ever touching the site definition files themselves. The Feature Stapling applies to all new instances of sites that use a given site definition, without modifying the site definition or creating code routines to activate the Feature on each site.  Your feature will be executed when the site is being provisioned.

How to start with?

Feature Stapling is achieved through the creation of another feature that defines the association of your regular Feature and the site definition you want to “staple” it too. In other words, you need to create two Features to achieve a complete Feature Stapling implementation.

Following is an example of feature stapling that associates the Feature with only the STS site definition templates.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#0" />
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#1" />
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="STS#2" />
</Elements>

Following is an example of feature stapling that associates the Feature with all site definitions.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id="00BFE171-1B17-4F72-28CB-1171C0140130" TemplateName="GLOBAL" />
</Elements>

This Feature Staplingshould be used when the site definition is already in use (and sites have been created). This is because there are risks in modifying a site definition once it has been deployed, since the site definition files on the filesystem are used for pages which are ghosted (not modified).

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. The, you should create a 2nd feature which does the actual stapling. Sample values for the various files are shown below:

To make sure that your new Site Definition has all the right pieces and parts, navigate to the \12\Template\Features\BaseSiteStapling folder and open BaseSiteStapling.xml in Notepad.

It should look something like this:

< Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”STS#0″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”STS#2″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”MPS#0″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”MPS#1″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”MPS#2″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”MPS#3″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”MPS#4″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”WIKI#0″ / >
< FeatureSiteTemplateAssociation Id=”B21B090C-C796-4b0f-AC0F-7EF1659C20AE” TemplateName=”BLOG#0″ / >

Take note of the first child node – it defines a Feature (in this case, the BaseSiteFeatureTitle) to be included with the site identified by the TemplateName setting.

The manifest.xml file if you are wrapping your feature in a solution:

The FeatureSiteTemplateAssociation element maps feature GUIDs to site defintions.

Note that the format of the TemplateName attribute value is <SiteDefName>#<ConfigurationID>. This obviously allows a degree of flexibility and allows you to do fairly complex things with different configurations of site definitions.

The final thing at the begining to note is that 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, though obviously an STSADM script can help you activate multiple features against multiple sites.

Feature Event Receiver

Another very cool thing to do with this is to implement features that have recievers (a receiver is a piece of code that runs when the feature is activated/deactivated). If you write some code into a receiver, it will effectivly run whenever a site of the given template is created, perfect for doing all sorts of things that feature’s wont normally do for you, so your new site really can be customised to be just like you need it to be – all without modifying the site definiton.

The Feature Event Receiver specifies a server-side code routine that is called as part of four key events in the lifetime of a Feature:

installation, activation, deactivation, and removal.

More Link:

http://www.sharepointblogs.com/jasonmedero/archive/2007/08/13/site-definition-customizations-versus-using-feature-stapling-part-2.aspx

Custom Code OnSiteCreation


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: