Search This Blog

Tuesday, September 2, 2014

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.


No comments:

Post a Comment