Tuesday, May 4, 2010

Feature stapling

If you have created a site definition, there are several ways of using features with it. These are:-
  1. Create a site from the definition and subsequently deploy your features to the site.
  2. Include the features in the site definition (onet.xml) before using it to create sites.
  3. Use feature stapling to associate your features to the site definition. 
Copy-pasted from a short and crisp article about feature stapling 

After extensive use of  the feature stapling  - I have discovered  that  the order of features in the staple feature is not GUARANTEED. Stapling features fires up when site is being created from certain site template configurations. Once the staple is fired up it runs Activating event all features that are listed under it. And because an Activating event is asynchronous all features are running pretty much at the same time, and who knows which features will be after which.

In my case I really need to know the exact order of execution.

My scenario this one: I have on prod a  site which was created from the site template. Once a while we need to deploy release for it. We stick with a procedure to make structure changes from OM(object model). So every release is a new code feature which has to be run over the existing site. To make possible to create a new site from the same site template and still have all the release changes - the feature stapling comes to play -it holds all feature releases which were created after the first release. At some point the latest release features expects the existence the changes made before it by older release.
As we know the feature stapling won't assure the exact order. My approach is to have only one feature in the stapling feature with name "ReleaseActivator":
 And here is the ReleaseActivator feature itself:

 And the custom code inside does the order magic:
I have re-factored the concept and instead of using stsadm I use Activator.Instance(t) :

and the type I put in the feature property:
(The value attribute is not in use, but I keep it just in case).

Good luck with SharePoint! You will need it.