Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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();
}
share|improve this answer
    
Aha, publishingweb does allow it. msdn.microsoft.com/en-us/library/… –  Cameron Verhelst Mar 20 at 7:04
    
Does it work even if subsites aren't publishing webs ? –  Cameron Verhelst Mar 20 at 7:04
    
Nadeem, did you use this code in feature activated? –  variable Mar 20 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. –  Nadeem Yousuf Mar 20 at 7:14

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

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

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

share|improve this answer
    
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 Mar 20 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/… –  Cameron Verhelst Mar 20 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 Mar 20 at 7:26
    
I cannot see this option in SP2013???? –  variable Mar 20 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 Mar 20 at 8:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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