Feature Stapling in SharePoint 2010 Part II

 

At this point Feature 1 is complete and we are ready to deploy to our sharepoint environment. You should have a solution explorer similar to this:

 

 

After you deploy, you can verify that everything worked by doing the following. Navigate to Site Actions -> Site Settings -> Site Collection Administration -> Site Collection Features. Search for the feature you deployed. You should find that it’s activated and working as expected.

The second thing we need to check is whether our master page was deployed or not. Navigate to  http://[RootSite]/_catalogs/master/Forms/AllItems.aspx in your browser. You should find that Test.master (or whatever the name you gave) is now visible and approved in our master page list.

At this point we have feature 1 fully working. We won’t stop here because we want this master page to be applied whenever a new site collection is provisioned. So we have to create our Stapler (Feature2) which will associate Feature1 with the site definition of My sites.

 

Step 3: Create Stapler Feature to associate Feature1 with a site Definition

Follows the same steps above to create another Feature called Feature 2. The scope of this feature should be set to Farm level.

Next add an empty element item called Elements.

Your Solution explorer should like this now

The next task is the most important since this involves associating feature with a site definition.

We will need the GUID from Feature1 . You can find this in the manifest.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation TemplateName="SPSMSITEHOST#0" Id="493a82e4-f05b-4244-91df-c99ba69db085"></FeatureSiteTemplateAssociation>
<FeatureSiteTemplateAssociation TemplateName="SPSPERS#0" Id="493a82e4-f05b-4244-91df-c99ba69db085"></FeatureSiteTemplateAssociation>

</Elements>

Two lines of code were added to make the association between feature and site defintion. The site definition id is encapsulated in the templatename [SPSMITEHOST#0 & SPSPERS#0].

We’ve now successfully created our feature and stapler. Deploy this to your SharePoint instance to see this in action.

 

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">