(I just started working with SharePoint last week excuse me if I am using some Names wrong)

I will try to explain what I have to do.

I have to change the design only of the main page of the master page

For testing I just inserted a <style> section inside of settle.master.

Now I have the final design but of course what I did is applying to all SharePoint sub sites what I don't want.

The design should be activated as Site Feature

Example of what is inside my css

/*Hide ShrePoint left navigation*/
.ms-core-navigation { DISPLAY: none }
#contentRow > #contentBox { margin-left: 0px; margin-right: 0px; }  

/*Change top SharePoint bar color blue to red*/
#suiteBar > #suiteBarLeft {background-color: red}

What I want is that this Changes are only applied to one page and the rest should use the default SharePoint design.

I am quite lost what I have to do now. Hope I could explain my issue correctly.

  • You want this to apply only on one page or only on root site master page? – Arsalan Adam Khatri Feb 20 '17 at 16:43
  • You should not edit the Out of the box Master, instead download it, create a copy, edit and rename it, and than upload it to Master Page library. And apply it on site, while applying don't check the box "Subsites inherit same master page setting" – Arsalan Adam Khatri Feb 20 '17 at 16:44
  • I am doing it like that frederik.se/…. How can I Upload it and it has to be a Site Feature that can be Activated and deactivatet – user53548 Feb 20 '17 at 16:57
  • In the frederik.se example, it will work like you said.. but in the code he applying master page to all subsites, I will post the code changes to an answer to fix it – Arsalan Adam Khatri Feb 21 '17 at 8:25

Instead of the below code:

// Enumerate through each site and apply branding.
foreach (SPWeb web in site.AllWebs)
     // Activate the publishing feature for all webs.
     web.Features.Add(new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"), true);
     web.MasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";
     web.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";


Use the following:

var rootWeb = site.RootWeb;
rootWeb.Features.Add(new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"), true);

rootWeb.MasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";
rootWeb.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";


This will change only the master page for Root web and not all the subsites within the site collection.


If you only want to apply Master Page on the web for which the feature is activated than following code should help (keep feature web level):

var webOnWhichFeatureActivate = properties.Feature.Parent as SPWeb;

SPWeb rootWeb = webOnWhichFeatureActivate.Site.RootWeb;

// Calculate relative path to site from Web Application root.
string webAppRelativePath = rootWeb.ServerRelativeUrl;
if (!webAppRelativePath.EndsWith("/"))
  webAppRelativePath += "/";

webOnWhichFeatureActivate.MasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";
webOnWhichFeatureActivate.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/MyCustom.master";

  • It works but only if the scope is set to Site I need the scope to be Web. What do I have to change what is the difference. If the scope is set so Web the master Page is uploaded and I can find it in sharePoint but nothing happens when I activate/deactivate the site feature. – user53548 Feb 21 '17 at 10:30
  • I think it shouldn't only affect the root web I think it should affect only where is activated. I am not sure but I think that was the reason why it has to be a sitefeature and not a sitecollection feature – user53548 Feb 21 '17 at 10:35
  • Actually AFAIK, the Master Page itself will only host in the root web's Master Page gallery.. only than it will work.. however, you can choose it to only apply on web for the feature it is activated.. I will update the code for the same – Arsalan Adam Khatri Feb 21 '17 at 11:17
  • @socramm check the updated code, the Module for deploying Master page will remain the same, the feature will be WEB level and this code should only change master page for which the feature is activated. – Arsalan Adam Khatri Feb 21 '17 at 11:23
  • 1
    thanks a lot. For the Deactivation I also copied your code but changed MyCustom.master to settle.master. Is this the correct way of doing it? – user53548 Feb 21 '17 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy