1

Here is my code on feature activated:-

    SPWeb topLevelSite = properties.Feature.Parent as SPWeb;
    if (topLevelSite != null)
    {


        // Calculate relative path to site from Web Application root.
        string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
        if (!WebAppRelativePath.EndsWith("/"))
        {
            WebAppRelativePath += "/";
        }

        // Enumerate through each site and apply branding.

        topLevelSite.MasterUrl = WebAppRelativePath +
                             "_catalogs/masterpage/InternalM1.master";
        topLevelSite.CustomMasterUrl = WebAppRelativePath +
                                   "_catalogs/masterpage/InternalM1.master";

        topLevelSite.Update();


    }

The scope of feature if set to 'WEB' then masterpage gets applied to the site. However, I want to set the scope to SITE COLLECTION ('SITE') so that all the subsites belonging to that site collection inherit the masterpage. If I do this then the masterpage doesn't get applied.

Am I doing any mistake?

3 Answers 3

2

If it is a Publishing web site then you can use SetInherit so that subsites will inherit the custom master page. Example:

SPWeb web = SPContext.Current.Web;
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
publishingWeb.CustomMasterUrl = "/_catalogs/masterpage/mycustom.master";
publishingWeb.CustomMasterUrl.SetInherit(true, true);
publishingWeb.Update();

This is the code I have in one of my projects site collection event receiver for setting the master page:

SPSite site = (SPSite)properties.Feature.Parent;
if (site != null)
{
    SPWeb web = site.RootWeb;           
    Uri masterUri = new Uri(web.Url + "/_catalogs/masterpage/mypractice.master");
    web.MasterUrl = masterUri.AbsolutePath;
    web.CustomMasterUrl = masterUri.AbsolutePath;
    web.Update();
}
4
  • Aha, publishingweb does allow it. msdn.microsoft.com/en-us/library/… Commented Mar 20, 2014 at 7:04
  • Does it work even if subsites aren't publishing webs ? Commented Mar 20, 2014 at 7:04
  • Nadeem, did you use this code in feature activated?
    – variable
    Commented Mar 20, 2014 at 7:12
  • Yes this code is used in site scoped feature in feature activated event. If the site is not a publishing web then use the code by @CameronVerhelst and beware to dispose each SPWeb object. Commented Mar 20, 2014 at 7:14
2

I do believe Site Collection scope is correct for a masterpage feature.

The problem is, you cannot force subsites to use this masterpage (even though there's an option to reset all subsites in the UI). EDIT: If it's a publishing web you can do this, look at @Nadeem Yousuf's answer

You will have to iterate over all the current SPWeb subsites and set their masterpage to inherit manually. Something like this:

                // SPWeb web = current subsite in loop
                using (SPWeb parent = web.ParentWeb)
                {
                    web.AllowUnsafeUpdates = true;
                    web.MasterUrl = parent.MasterUrl;
                    web.CustomMasterUrl = parent.CustomMasterUrl;

                    web.SiteLogoUrl = parent.SiteLogoUrl;

                    web.Update();
                    web.AllowUnsafeUpdates = false;
                }

If you want new subsites to inherit, I think you need to have a feature for it as well, that gets automatically activated. You could do it with feature stapling (to a site definition, or all site definitions) or perhaps the WebProvisioned event receiver

5
  • Hi Cameron, What if I use this code:-foreach (SPWeb web1 in site.AllWebs) { //paste the code to set masterpage here }
    – variable
    Commented Mar 20, 2014 at 7:07
  • Will it then apply masterpage to each subsite? Isnt that logical?
    – variable
    Commented Mar 20, 2014 at 7:08
  • That would work, don't forget to dispose the SPWeb's created this way. Commented Mar 20, 2014 at 7:08
  • Then, Is it OK to use this code in the feature activated?
    – variable
    Commented Mar 20, 2014 at 7:08
  • Yup, but if it's a publishing web @Nadeem Yousuf's answer is cleaner Commented Mar 20, 2014 at 7:09
1

Scope of masterpage feature should be set to 'Site'. You need to set the Reset all sub sites option for the masterpage to be applied to the sub sites.But also the sub sites will inherit the new master only if Site collection feature "SharePoint Server Publishing Infrastructure" is activated and in the sub sites the site feature "SharePoint Server Publishing" is activated.

SPWeb topLevelSite = properties.Feature.Parent as SPWeb;
    if (topLevelSite != null)
    {


        // Calculate relative path to site from Web Application root.
        string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
        if (!WebAppRelativePath.EndsWith("/"))
        {
            WebAppRelativePath += "/";
        }

        // Enumerate through each site and apply branding.

        topLevelSite.MasterUrl = WebAppRelativePath +
                             "_catalogs/masterpage/InternalM1.master";
        topLevelSite.CustomMasterUrl = WebAppRelativePath +
                                   "_catalogs/masterpage/InternalM1.master";

        topLevelSite.Update();


    }

Next go to Site Settings--->MasterPage-->check "Reset all subsites to inherit this site master page setting".To programmatically perform theabove step refer this thread

5
  • Does it depend upon the requirements whether we set scope of master page feature to site/web? Or is it mandatory to set it to site? (it does work when we set to web also)
    – variable
    Commented Mar 20, 2014 at 6:58
  • SPWeb.CustomMasterUrl is a string property. How can you call SetInherit on it? I can't find an msdn reference. msdn.microsoft.com/en-us/library/… Commented Mar 20, 2014 at 7:00
  • edited that.You can remove that line and do that step manually, by going to Site Settings--->MasterPage-->check "Reset all subsites to inherit this site master page setting"
    – Unnie
    Commented Mar 20, 2014 at 7:26
  • I cannot see this option in SP2013????
    – variable
    Commented Mar 20, 2014 at 7:34
  • Check whether Site collection feature "SharePoint Server Publishing Infrastructure" and site feature "SharePoint Server Publishing" is activated.If they are activated then you can see Master Page opetion in look and feel section of Site Settings
    – Unnie
    Commented Mar 20, 2014 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.