-->

26/03/2013

Deploying Custom Master Page Using a sharepoint Feature

In this module, we will see how to deploy a Custom master page to a sharepoint site, using a sharepoint feature.

Step 1: Create a sharepoint project. Add a Module to it. I named it as "MasterModule". By default the module will be created with 2 files,  Elemnts.xml and Sample.txt.

Step 2: You can add / create a Custom Masterpage file as per your requirment. For my purpose, i am trying to reuse the existing V4.Master.

If you are trying to export the master page from Sharepoint Designer, you may end up with error

http://server/_catalogs/masterpage/v4_Modified.master(26): error CS0030: Cannot convert type 'Microsoft.SharePoint.WebControls.ScriptLink' to 'System.Web.UI.IAttributeAccessor'
This happens when you use SaveAs option in designer to export V4.Master file. To avoid this, Just Open V4.Master file in Designer, copy all the markup and put it in a text file and rename it to V4.Master. Use this file Instead.
Or
Got to "Site Settings" -> "Master Page Gallery" -> Right click on page and Select "Send To" ->"Download a Copy" options to save the file to you local disk.

I just added a Header part saying "Pratap's Modified Master" to differentiate the modified master from regular master.

Step 3: When you add the module, by deafult a new Feature will be added, else add a new feature and add the module to that feature. I named the feature as "Site.MasterPageFeature".


Step 4:Modify the content of Elelments.xml so that the Masterpage will be Deployed to "_Catalog/MasterPage" library.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MasterModule" List="116" Url="_catalogs/masterpage">    
  <File Path="MasterModule\v4_Modified.master" Url="v4_Modified.master" Type="GhostableInLibrary" />
</Module>
</Elements>

Step 5: Now when you deploy the solution, the custom master page will be deployed, but its not assigned as default pasterpage for the site. You can do it manually, but it is not the preffered way. So, in order to acheive this programatically, we need to use the Event Receiver for this task.

Right click on Feature and select "Add Event Receiver" option. Now, below is the code for changing the Master page programatically. We will be using events "FeatureActivated" and "FeatureDeActivated" events for this.

   [Guid("be79ba52-669c-4d89-826c-fe4abf2dceaa")]
    public class SiteEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var site = (SPSite)properties.Feature.Parent;
            site.RootWeb.MasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4_Modified.master";
            site.RootWeb.CustomMasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4_Modified.master";
            site.RootWeb.Update();
        }
        // Uncomment the method below to handle the event raised before a feature is deactivated        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var site = (SPSite)properties.Feature.Parent;
            site.RootWeb.MasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4.master";
            site.RootWeb.CustomMasterUrl = site.RootWeb.ServerRelativeUrl + "_catalogs/masterpage/v4.master";
            site.RootWeb.Update();
        }
    }

Now, deploy the solution and Activate the "Site.MasterPageFeature" from site collection features, the site will be transformed to this, using your custom master page.


 Code Sample:

Search for "SPDelegateProject.zip" in here. This sample will have code for both Custom Master page and for Delegate Control as well.

1 comment:

  1. Hi, I found your blog very useful! Especially the files you host on your SkyDrive.
    I am in a real need here. I need a SharePoint 2013 solution that is a default, OOTB seattle.master with all the design files. Mine is corrupt and I cannot reset to site definition nor do anything else. i am forced to use oslo.master which in my case is unsuitable. The task is simple enough, I know and I even have a copy of VS2013. The problem I have there is I cannot install sharePoint on the machine running Visual Studio. Help help help!

    ReplyDelete