Sunday, 28 September 2014

SharePoint 2010 - Features Stapling

Feature stapling allows a Feature to be stapled to any new sites created from any site definition or from specific site definitions based on the template name identified in the appropriate WEBTEMP.xml file. The stapled feature will be automatically activated each time a new site or sub-site is created using the associated site definition.

Warning: You can not use feature stapling on a site created using a site template (.stp) file.  If you try to staple feature on a site which was created using site template (.stp) file, the feature will not get activated automatically when the new site is created.

You can staple a feature to a site created using any of the in-built site definition templates or your own custom site definitions. For instance, following are some of the main site definitions available in SharePoint.
Template Name
Template Title
STS#0       
Team Site
STS#1      
Blank Site
STS#2      
Document Workspace
MPS#0      
Basic Meeting Workspace
MPS#1      
Blank Meeting Workspace
MPS#2      
Decision Meeting Workspace
MPS#3      
Social Meeting Workspace
MPS#4      
Multipage Meeting Workspace
WIKI#0     
Wiki Site
BLOG#0     
Blog Site
Feature stapling Components
Feature stapling consists of 3 main components:
1) Stapler Feature: The parent feature which has a higher scope than the Staplee or Child feature.
2) Staplee Feature: The Child feature which gets activated when the Stapler feature gets activated.
3) Stapler Feature's Element.xml: Consist the association between the Staplee feature's Id and the Site definition template name (eg: STS#0) on which it will get activated.
<FeatureSiteTemplateAssociation Id="b94f446e-0989-4d90-8abb-7aa0704f4506" TemplateName="STS#0">

Feature stapling Scenario
We need to create multiple sub sites inside a site collection with template as team site. Each time a new sub-site is created, it must have a list named Team Docs which is automatically created at the time of creation.

Feature stapling Implementation
We can use Feature Stapling in this scenario as follows:
1) Create a Stapler feature scoped to Site level.
2) Create a Staplee feature scoped to the Web level. Also add a feature reciever and write the code for creating a new list named "Team Docs" at the time of activation.
3) Associate the Staplee feature with the Team Site template Name ("STS#0") in the Elements.xml file.
4) Deploy the solution and Install both the features.
5) Activate the Site collection level Stapler feature. The Web level Staplee feature will be automatically stapled.
6) Whenever a new sub-site based on Team Site template is created, a list named Team Docs will be automatically created.

Feature stapling Demo
Following are the steps:-

Create an empty SharePoint 2010 project
Enter your site collection url and select "Deploy as a farm solution" option.

Add a new feature named "FeatureStaplingDemo Stapler" with scope as Site.

Add another new feature named "FeatureStaplingDemo Staplee" with scope as Web.
Now, right click on project and add an Empty Element to the project.

Go to web scoped Staplee feature and copy the feature id from the feature manifest.

Add the following code in the FeatureSiteTemplateAssociation tag in Elements.xml where
id is the feature id of the staplee and
TemplateName is the name of Site template on which we are stapling this feature.

Code for Elements.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="b94f446e-0989-4d90-8abb-7aa0704f4506" TemplateName="STS#0"></FeatureSiteTemplateAssociation>
</Elements>
Now, add a feature receiver to the web scoped Staplee feature.

In the feature activated event, add the code for adding a new list named "Team Docs" whenever a new sub-site is created.

Code for Feature Activated and Deactivating
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
                SPWeb web = properties.Feature.Parent as SPWeb;
                try
                {
                    SPList targetList = web.Lists["Sales"];
                }
                catch (ArgumentException)
                {
                    Guid listId = web.Lists.Add("Team Docs""Team Documents"SPListTemplateType.GenericList);
                    SPList list = web.Lists[listId];
                    list.OnQuickLaunch = true;
                    list.Update();
                }                
        }       
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            try
            {
                SPList list = web.Lists["Team Docs"];
                list.Delete();
            }
            catch (ArgumentException)
            {
            }    

        }

Deploy the solution

In the site collection, go to Site Setting->Site Collection Features. You can see that the Stapler feature has been activated.

Now, create a new sub-site with template as Team Site.

You will notice that the List named "Team Docs" has been automatically created.

Go back to Site Setting->Site Collection Features in the site collection and deactivate the Stapler feature.

Create another sub-site with the template as Team Site.

This time the list named "Team Docs" is not created since the Stapler feature is deactivated.

Hope this post helps!

No comments:

Post a Comment