While working on an internal project recently, I was tasked to implement feature stapling which would enable us to load a custom master page to the My Site (specifically My Content) section. Before we jump into all the details I would like to define some terms just in case there are newbies (like me) who had no idea about how to approach feature stapling.
Feature Stapling:
Feature stapling is a technique used in SharePoint 2010 which allows you to associate a feature (say a custom master page feature) with a site collection or web site (My sites) without modifying the site definition. In this tutorial the custom master page will be deployed to the new site collection whenever it’s provisioned for the first time.
Feature: A SharePoint feature provides additional functionality to SharePoint. This feature can be used once deployed and activated on the SharePoint site. Technically a SharePoint feature is just an XML file (Feature.xml). In our tutorial we will create two features.
- The first feature (Feature1) will define the new master page we want to apply.
- The second feature (Feature2) will establish the relationship between feature 1 and our site definition.
Module: A module is SharePoint template which can be used to deploy files to your SharePoint environment. We’ll create a module to deploy our custom master page.
Summary:
Setup project in Visual Studio
Create Feature 1 (Custom master page)
Create Stapler Feature to associate Feature1 with a site Definition
Step 1: Setup Project
Add a new project to your solution called ‘’MySiteFeatureStapling” and click ok.

Next, select the security level for your site to “Deploy as a farm solution” and click finish. You can choose a sandboxed solution if you so choose. They should both work.

At this point your solution explorer should look something like this.

Step 2: Create Feature 1 (Custom Master Page)
Right click the Features folder and click on “Add Feature”

You will then be provided with the design interface of the feature designer. You can choose to rename your feature to something more meaning if you wish. You should at this point set the scope to “Site” .

Add Module
Next, you need to right click on your project in solution explorer to add a new module. This module will be used to deploy the custom master page. Delete the sample.txt file which is provided by default and add the custom master page (in this case Test.master) you would like to deploy. Update the Elements.xml file to look like this.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Module1" List="116" Url="_catalogs/masterpage">
<File Path="Module1\Test.master" Url="Test.master" />
</Elements>
</module>
The URL attribute in the module node specifies where the file will be pushed on your SharePoint server. It’s important to note that you can add any type of file you want to the module. Add Feature Event Receiver : The Next step is to right click feature1 and add an event receiver. A new file Feature1.EventReceiver.cs is created with a bunch of commented out code. Uncomment the methods FeatureActivated & FeatureDeactiving. Modify your code to look like this
[Guid("f5034122-c5af-4327-ad77-b52b1a107667")]
public class Feature1EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb curWeb = site.RootWeb;
//create full master url
Uri masterURI = new Uri(curWeb.Url + "/_catalogs/masterpage/Test.master");
//Master page used by all forms and pages on the site that are not published
curWeb.MasterUrl = masterURI.AbsolutePath;
//master page used by all publishing pages on the site
curWeb.CustomMasterUrl = masterURI.AbsolutePath;
curWeb.Update();
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb curWeb = null;
SPSite curSite = properties.Feature.Parent as SPSite;
if (curSite != null) { curWeb = curSite.RootWeb; }
if (curWeb != null)
{
//Create full master url
Uri masterUri = new Uri(curWeb.Url + "/_catalogs/masterpage/v4.master");
//master page used by all forms and pages on the site that are not publishing pages
curWeb.MasterUrl = masterUri.AbsolutePath;
//master page used by all publishing pages on the site
curWeb.CustomMasterUrl = masterUri.AbsolutePath;
curWeb.Update();
}
}
Click below for the next part of this tutorial
Feature Stapling in SharePoint 2010 Part II