Monday 22 December 2014

Feature Stapling in SharePoint


I want a custom list to be created by default whenever a site is created using the Team Site template. The best way to do this is to create a Feature Stapling. 

Feature Stapling

Feature Stapling can be created for the site definition that is already in use. Feature Stapling has two features.
  1. Stapler feature: staples another feature to the site definition
  2. Staplee feature: that which will be stapled
Steps involved
The following is the procedure steps to to implement Feature Stapling in SharePoint:
  1. Create Empty SharePoint Project
  2. Create a feature
  3. Create the feature stapler
  4. Create FeatureAssociation.xml
  5. Deploy the solution
  6. Check whether the feature stapler is activated in a web application
  7. Testing
Create Empty SharePoint Project
  1. Open Visual Studio 2010 by going clicking "Start" then select "All Programs" | "Microsoft Visual Studio 2010" then right-click on Microsoft Visual Studio 2010 and click on "Run as administrator".
  2. Go to the File tab, click on "New" and then click on "Project".
  3. In the New Project dialog box, expand the Visual C# node, and then select the SharePoint 2010 node.
  4. In the Templates pane, select "Empty SharePoint Project".
  5. Enter the Name as FeatureStapling and then click "OK". 

    Share1.jpg
     
  6. Enter the local site URL for debugging, select "Deploy as a farm solution" as in the following and then click on "Finish".

    Share2.jpg
Create a feature
In this section you will see how to create a new feature and feature receiver to create a custom list in the site.
  1. In the Solution Explorer, right-click on the Feature folder and then click on "Add Feature".

    Share3.jpg
     
  2. Rename the feature to "CreateListFeature".
  3. Double-click on the feature and enter the Title and Description for the feature.

    Share4.jpg
     
  4. Select the scope as web from the drop down list.
  5. Right-click on CreateListFeature and then click on the Add Event receiver.

    Share5.jpg
     
  6. Double-click on CreateListFeature.EventReceiver.cs and replace the code with the following:

    Share6.jpg
     
  7. The code above creates a custom list on the feature activated.
Create feature stapler
In this section you will see how to create a feature stapler that is used to staple the CreateListFeature to the site definition.
  1. In the Solution Explorer, right-click on the Feature folder and then click on "Add Feature".

    Share7.jpg
     
  2. Rename the feature to "FeatureStapler".

    Share8.jpg
     
  3. Double-click on the feature and enter the Title and Description for the feature.

    Share9.jpg
     
  4. Select the scope as WebApplication from the drop down list.
Create FeatureAssociation.xml
In this section you will see how to create the XML file to associate the feature to the site definition.
  1. In the Solution Explorer, right-click on the project, click on "Add" and then click on "New Item".
  2. Select the "Empty Element" template, enter the Name and then click on "Add".

    Share10.jpg
     
  3. Double-click on Elements.xml and replace with the following:

    Share11.jpg

    Id - CreateListFeature feature Id.

    Template Name: To which site template the feature should be associated (the format should be <site template name><configuration number>).
     
  4. Ensure this file is added to the FeatureStapler feature (double-click on the FeatureStapler feature and see whether in the "Items in the feature" window the Elements.xml file is available).

    Share12.jpg
Deploy the solution
Right-click on the solution and then click on "Deploy Solution".
Check whether the feature stapler is activated in the web application:
  1. Open Central Administration.
  2. Click on Manage Web Applications that is available in the Application Management section.
  3. Select the web application and then click on "Manage Features" in the ribbon interface.

    Share13.jpg
     
  4. Check whether the Feature Stapler feature is activated.

    Share14.jpg
Testing
  1. Open Central Administration.
  2. Click on "Application Management".
  3. Click on "Create Site Collections" that is available under the Site Collections section.
  4. Create a new site collection using the Team Site template.
  5. Navigate to the newly created team site.
  6. Click on "All Site Content" in the quick launch bar.
  7. You will see the custom list created by default.

    Share15.jpg
Summary

Thus in this article we have seen an example for implementing the Feature Stapling in SharePoint.

No comments:

Post a Comment