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 can be created for the site definition that is already in use. Feature Stapling has two features.

  • Stapler feature: staples another feature to the site definition
  • Staplee feature: that which will be stapled

Steps involved:-

The following is the procedure steps to to implement Feature Stapling in SharePoint:

  • Create Empty SharePoint Project
  • Create a feature
  • Create the feature stapler
  • Create FeatureAssociation.xml
  • Deploy the solution
  • Check whether the feature stapler is activated in a web application
  • Testing

Create Empty SharePoint Project

  • 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”.
  • Go to the File tab, click on “New” and then click on “Project”.
  • In the New Project dialog box, expand the Visual C# node, and then select the SharePoint 2010 node.
  • In the Templates pane, select “Empty SharePoint Project”.
  • Enter the Name as FeatureStapling and then click “OK”.

featureStaplingPrj

  • Enter the local site URL for debugging, select “Deploy as a farm solution” as in the following and then click on “Finish”.

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.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj2

  • Rename the feature to “CreateListFeature”.
  • Double click and enter the title & description ofthe feature .
  • Select the scope as web from the drop down list.
  • Right-click on CreateListFeature and then click on the Add Event receiver.

featureStaplingPrj3

  • Double-click on CreateListFeature.EventReceiver.cs and replace the code with the following:
protected override void FeatureDeactivatingInternal(SPFeatureReceiverProperties properties)
{
     SPWeb web = (SPWeb)properties.Feature.Parent;
     string listUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "Lists/CertificateInformationTool")
     SPFolder folder = web.GetFolder(listUrl);
     if (folder.Exists)
     {
         SPList list = web.Lists[folder.ParentListId];

         list.Recycle();
     }
}
  • The Code above creates a list on the feature activation .

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.

  • In the Solution Explorer, right-click on the Feature folder and then click on “Add Feature”.

featureStaplingPrj4

  • Rename the feature to “FeatureStapler”.

featureStaplingPrj5

  • Double-click on the feature and enter the Title and Description for the feature.

featureStaplingPrj6

  • Select the scope as WebApplication from the drop down list.

Create FeatureAssociation.xml

Now we need to create the XML file to associate the feature to the site definition.

  • In the Solution Explorer, right-click on the project, click on “Add” and then click on “New Item”.
  • Select the “Empty Element” template, enter the Name and then click on “Add”.

featureStaplingPrj7

  • Double-click on Elements.xml and replace with the ListFeature feature Id.

                 Template Name : To which site template the feature should be associated (the                                                            format should be <site template name><configuration                                                                        number>).

  • 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).

featureStaplingPrj8

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.

Testing

Now whenever we go to any of the site collections and look for the particular feature, it should be activated .
Thus, we can have this list on any site collection in a particular web application when we have this feature activated and vice-versa.

featureStaplingPrj9

References

https://msdn.microsoft.com/en-us/library/vs/alm/bb861862(v=office.12).aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s