SharePoint 2010 – How to set default/custom master Page programatically using VS Studio

Recently i received number of request to post on “How to set custom/default master page programatically using Visual Studio 2010″. So this post is for those people who desperately waiting to have something into their plate  🙂

Goal: You can set a custom/default master page using SharePoint Designer 2010 as well. But you need to work on Designer for that. So to remove this dependency, one solution is to create a solution project which allows you to set custom/default master page programatically.

I am explaining all steps which deploys the custom/default master page and set as default as well. The steps are as below:

1. Create module and add master page

2. Set added master page as default master page

1. Create module and add master page:

  • Open Visual Studio 2010 –> Add New Project –> Select “Empty SharePoint Project” –> Select “Deploy as Farm Solution” –> Click on “Finish”
  • Right Click on Solution and select Add new item –> Select “Module” and name it what you want. (In my case i used CustomMasterPage)
  • Delete “Sample.txt” file or rename it with “MyCustomMasterPage.master”
  • Open “Element.xml” file of “CustomMasterPage” module.
  • Element.xml file looks like below after doing some changes :

  • Please make sure you have entered below values and deploy your solution

      List=116Url=_catalogs/masterpage”    and     Url=MyCustomMasterPage.masterType=GhostableInLibrary

2. Set added master page as default master page

  • From solution project select “Features –> Feature1 –> Feature1.feature” –> Right click “Add event receiver”
  • Add below mentioned code and deploy solution.

You are done with the mentioned goal.

But there is still one issue. When you need some changes into custom master page and want to re-deploy it, Your updated master page would not get deployed. So to resolve this issue, we need to write de-activating event which reset V4.master as default master page. Add below mentioned code in FeatureDeactivating event.

After adding this code Deploy your solution and this will reset V4.master page as default.

Hope this helps you guys.

3 comments

  1. Hi, I wanted to ask if there’s a way to set programmatically a device channel’s masterpage in SP2013
    I’m new to SP development but still I can’t find any references about this issue.

    Looking forward to your answer,
    Nico

    1. Hi Nicoleta,

      Yes. That is possible by creating custom solution along with feature which deploy custom master page.

      Thanks,
      Kirti.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s