STEP 1 : Adding a Master Page using Module
- Open a visual studio 2010
- Add new project
- Empty Sharepoint Project > Deploy as Farm Solution > Finish.
- Add New Item > Select "Module" > I named it as "MasterPage".
- Delete the "Sample.txt" which gets added in the module by default.
- Add new MainMasterPage.master. Do whatever change you want to it.
- 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>
- 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"
- Once this is set just deploy the project.
STEP 1.1 : Verify the file got uploaded
- Open the site in SharePoint Designer 2010.
- All Files > _catalogs > MasterPage
- 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
- In the visual studio solution project select "Features > Feature1 > Feature1.feature".
- Right click "Add event receiver".
- 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(); } }); }
- 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.
