Sharepoint 2013: Deploy a master page through a feature

It seems that the deployment code required for a masterpage in SharePoint 2013 is a little different from previous versions.

The same logic applies - create a module and deploy it with a feature.

Here is a sample module code, using my custom masterpage 'MyMaster.master'

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

And here is the code in the feature receiver on the FeatureActivated event.

SPSite site = properties.Feature.Parent as SPSite;
SPWeb curWeb = site.RootWeb;

//create full master url
Uri masterURI = new Uri(curWeb.Url + "/_catalogs/masterpage/MyMaster.master");

curWeb.MasterUrl = masterURI.AbsolutePath;
curWeb.CustomMasterUrl = masterURI.AbsolutePath;
curWeb.Update();


Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

Error deploying Nintex workflow: An item with the same key has already been added