Monday, September 26, 2011

SharePoint 2010 Deploy Master Page using Visual Studio Solution Project and Set it as Default Master Page.

This blog will give an understanding of how to add a master page using a Visual Studio solution project. Also setting the same master as a default for site.


STEP 1 : Adding a Master Page using Module

  1. Open a visual studio 2010
  2. Add new project
  3. Empty Sharepoint Project > Deploy as Farm Solution > Finish.
  4. Add New Item > Select "Module" > I named it as "MasterPage".
  5. Delete the "Sample.txt" which gets added in the module by default.
  6. Add new MainMasterPage.master. Do whatever change you want to it.
  7. Open the elements.xml file of "MasterPage" module. You will have to make some modification shown below.


    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <Module Name="MasterPage" List="116" Url="_catalogs/masterpage">
            <File Path="MasterPage\MainMasterPage.master" Url="MainMasterPage.master" Type="GhostableInLibrary">
                <Property Type="string" Name="Title" Value="My Main Master Page"/>
            </File>
      </Module>
    </Elements>
  8. Please verify the below given text i made change in the original one. This is MOST IMPORTANT!!!.
    List="116" Url="_catalogs/masterpage"
    Url="MainMasterPage.master" Type="GhostableInLibrary"
  9. Once this is set just deploy the project.

STEP 1.1 : Verify the file got uploaded

  1. Open the site in SharePoint Designer 2010.
  2. All Files > _catalogs > MasterPage
  3. The file should be at this location because if you see the element.xml above in the url we have given the path of "_catalogs/masterpage". That's it we now achieved the first step of adding a master file to the site.


STEP 2 : Apply the page as a default master page

  1. In the visual studio solution project select "Features > Feature1 > Feature1.feature".
  2. Right click "Add event receiver".
  3. Put this code.

    /// <summary>
    /// Set the MainMasterPage.master as default master for site.
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate
        {
            using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
            {
                Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/MainMasterPage.master", _web.Url));
                _web.MasterUrl = _siteMaster.AbsolutePath;
                _web.CustomMasterUrl = _siteMaster.AbsolutePath;
                _web.Update();                   
            }
        });
    }
  4. Right click the project and deploy the solution.

I hope this helps you with the deployment of master page and setting it as a default.

PROBLEM :- There is a problem with the current process. For the first time this will work but if you make any changes to the master page and re-deploy the same solution will not deploy this new master page with the changes on the server. Please read my next blog "Sharepoint 2010 Master page issue with re-deployment with new changes from Visual Studio solution" for the solution.

2 comments:

  1. One step remaining.The developer must go into SharePoint Designer and set the new master as the default master. This cannot be accomplished in the deployment in VS.

    ReplyDelete
    Replies
    1. @Clem,

      Thanks again for correcting there should be a masterpage in the project for this code to work.

      Thanks,
      Maulik Dhorajia

      Delete