Search This Blog

Monday, January 12, 2015

Feature Stapling in SharePoint

Scenario

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


                                                       OR

Feature Stapling in Sharepoint

CodeProjectFeature Stapling is a concept using which a specific feature will be stappled to site template.What we gona acheive with it?There will be scenario where certain resource has to be populated to every site collection that was / will be created on a farm. Eg : I am dealing a client who 
is a

Feature Stapling is a concept using which a specific feature will be stapled to site template.

What we gonna achieve with it?

There will be scenario where certain resource has to be populated to every site collection that was / will be created on a farm.

Eg : I am dealing a client who is a owning a chemical lab. He want complete list of "Elements and their Atomic weights" to be present in every site that was already created, and any site that will be created in future.

In this scenario, Feature stapling will be used.
Lets see how to demonstrate it.

I have created a sharepoint project in which there are 2 features.
1. Stapled Feature - The feature taking care of creation of the Fields, a Content type and a List from that specific Content Type.
2. Stapling Feature - The feature which will staple the "Stapled Feature" to specific Site template.

With Feature #1 : Stapled Feature (Site Scoped), LstContentType will be added to a site and a list called StapledList was getting created with default data i intended to create.

Stappled Feature Deployed and Activated

LstContentType
StapledList

Add a element to project and i names it "StaplingElement". Modify the elements.xml under StaplingElementand add the below XML tag to associate Stapled Feature to site template.
<featuresitetemplateassociation id="934cb12e-f397-41ff-be5a-7d32a085aff0" templatename="STS#0">

Now, create second feature #2 Stapling Feature (Farm Scoped), and add the Stapling Element.


Deploy the solution (WSP).
Go to Central Admin => System Settings => Manage Farm Features => activate Stapling Feature.

Now, create a new Site Collection under a Existing  / New Webapplication using "Team Site" template.

The "#1 Stapled Feature" will be automatically available in new site colection and will be activted. Thus all the above Fields, Content Type,  List and its data will be automatically created by default.


How Feature stappling will effect the existing sites?

#1 Stappled feature will be added to exiting sites, but we need to enable the feature in all the existing sites using powershell or manually.

Many of the developers might think different solutions like Custom Site TemplateWeb Provisioning orSeparate WSP with new feature , instead of Feature Stapling. But each solution has a unique scenario which we will discuss the comparison in next post.

Code Smaple: Downlaod Feature stapling.zip from skydrive folder. 



No comments:

Post a Comment