HOW TO: Change master page of a personal site in SharePoint 2013

This post is a contribution from Pavan Kumar, an engineer with the SharePoint Developer Support team.

As in SharePoint 2007 and 2010, one of the ways to achieve this is to use FeatureStapling. Below are the steps to be followed to get this working.

Create a SharePoint 2013 Empty project in Visual Studio 2012 and give it a name (e.g., ChangeMySiteMaster).

image

Provide your MySite Host URL and choose to deploy this as a farm solution.

image

Add a new Module from Visual Studio 2012 SharePoint item templates to the project.

image

By default, MySites use mysite15.master as its default master page.  This can be found under 15\TEMPLATE\FEATURES\MySiteMaster\mysite15.master.  Copy this file and add it to the project you created.  Rename it to CustomMySite.master and modify this file per your needs.

Delete Sample.txt available in the Module and edit Elements.xml file such that it looks like this:

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
   <Module Name="MasterPage" List="116" Url="_catalogs/masterpage" Path="MasterPage" RootWebOnly="TRUE">
     <File Url="CustomMySite.master" Type="GhostableInLibrary"/>
   </Module>
 </Elements>

Add a new feature to the project and add a feature event receiver to it.  Ensure that module is added to the feature.

image

image

Add below code in FeatureActivated event of EventReceiver.cs.

image

Add below code in FeatureDeactivating event of EventReceiver.cs.

image

Create another farm level feature.  Name it as MySiteFarmStapler.  Add a new empty element to the project and name it as ML_ApplyCustomMasterPageStapler.  Add the following markup to the XML.

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
   <FeatureSiteTemplateAssociation Id=<GUID from EventReceiver.cs>" TemplateName="SPSPERS" />
 </Elements>

Add the module to Stapler feature.

image

Save all the files and deploy.  Login to your personal site and activate the feature from Site collection features.  Personal sites created henceforth will have the new master page applied to it.