.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Feature Stapling in SharePoint 2010

Posted By:Jean Paul       Posted Date: August 30, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we can explore how to do Feature Stapling in SharePoint.
 

In this article we can explore how to do Feature Stapling.

What is Site Definition?

Site Definition consists of XML and ASPX files residing in the web server file system; they allow creation of site through pre-configured settings.

For example, you can create a Department Site Definition with:

1. One document library named Documents

2. One list named Contacts

3. One web part named Announcements

Later, this site definition can be used to create multiple department sites like Accounting Department, HR Department. The site instances will have the pre-configured uniform assets & behavior.

Scenario

We have a Site Definition with Contacts list.

Customers created site instances out of this Site Definition.

We need to add a Tasks list to the Site Definition.

How the new Tasks lists get integrated to existing site instances?

Solution 1: Update Site Definition

In this solution, the site definition is updated with the new Tasks list.

But, there are chances it will break the existing site instances. Hence not recommended.

Solution 2: Feature Stapling

Create a new feature & add the Tasks list to the feature. 

Staple the feature along with the previous Site Definition.

In this approach, whenever new site instances are created from our Site Definition, the new Tasks list get attached to it, through feature stapling.

But, existing site instances won't get updated.  We need to do the following:

1. Manual Creation of new list in existing site instances

2. Creating new list through code in the Feature Activation event on existing site instances

Following is the POC on Feature Stapling

Feature Stapling

Feature stapling is a technique that allows for a feature to be stapled to a site definition.  This allows the feature to be automatically activated on the Site Instance creation from Site Definition.  We can also Feature Staple along with SharePoint Out-of-the-box site definitions.

Feature Stapling consists of 2 Features:

1. Staplee Feature The feature which we attach to the Site Definition.

2. Stapler Feature The feature that will actually do the Stapling.

Please follow the steps below to achieve Feature Stapling.

We are going to attach a  My Tasks list for all future Team Site instances.

Step 1 Create Project

image

Choose the Farm Solution option.
image

Step 2: Add Staplee Feature

Here we are adding the feature which will create the My Tasks list.

image

 

Step 3 Add Event Receiver

Right click on the feature & choose Add Event Receiver

image

In the appearing code file, uncomment the Feature Activated event, add the following code.

public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

SPWeb web = (properties.Feature.Parent as SPWeb);

Guid guid = web.Lists.Add("MY TASKS", "Created through Feature Stapling", SPListTemplateType.Tasks);

SPList list = web.Lists[guid];

            list.OnQuickLaunch = true;

            list.Update();

        }

The code creates the MY TASKS list & add to the quick launch.

Step 4 Add Stapler Feature

This feature will associate the above feature with the site definition.

image

Please note that we choose the scope as Web Application.

Step 5 Add Empty Element

Add an empty element to the project & rename it as FeatureStaplerElement.

image

Replace the content of XML file with the following.

<?xml version="1.0? encoding="utf-8??>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#0? />

  <FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#1? />

  <FeatureSiteTemplateAssociation Id="8a959d93-662a-4a25-8fb5-1407362eb9f5? TemplateName="STS#2? />

</Elements>

The code basically adds the MY TASKS feature to the site definitions of templates Team Site, Blank Site etc. using the IDs respectively.

(In your case please use your Site Definition ID)

Note The feature id can be received using the Properties Window.

Our My Tasks Feature looks like this:

image

Our Stapler Feature looks like this.

image

Step 6 Deploy Solution

Right click on the solution & choose Deploy option.
image

Step 7 Testing the Solution

Go to Central Administration & Create a new site collection of type Team Site.

You will be seeing the new site collection with our MY TASKS list as shown below.

image

This concludes our Feature Stapling demonstration.

References

http://msdn.microsoft.com/en-us/library/bb861862%28v=office.12%29.aspx

Summary

In this article we have explored Feature Stapling scenario.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend