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

I am developing a branding project, in this project i have custom master page, i can successfully deploy it and automatically apply to the root web. But i want all the subsites inherit the master page programmatically(From feature event receiver)

But the;

rootWeb.AllProperties["__InheritsCustomMasterUrl"] = "True";

is not working. My master page is not being applied to the subsites. Do you have any ideas why?

Edit: Here is my full code. It still doesnt work

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        var site = properties.Feature.Parent as SPSite;

        if (site != null)
        {
            SPWeb rootWeb = site.RootWeb;

            string serverRelativeUrl = rootWeb.ServerRelativeUrl;
            if (!String.Equals(serverRelativeUrl, ForwardSlash, StringComparison.OrdinalIgnoreCase))
                serverRelativeUrl += ForwardSlash;


            string customMasterUrl = serverRelativeUrl + "_catalogs/masterpage/" + DefaultMasterPage;

            bool update = false;

            if (!String.Equals(rootWeb.CustomMasterUrl, customMasterUrl, StringComparison.OrdinalIgnoreCase))
            {
                rootWeb.CustomMasterUrl = customMasterUrl;
                rootWeb.AllProperties["__InheritsCustomMasterUrl"] = "True";
                update = true;
            }

            if (update)
                rootWeb.Update();

            string customSearchMasterUrl = serverRelativeUrl + "_catalogs/masterpage/" + SearchMasterPage;

            foreach (SPWeb web in site.AllWebs)
            {
                if (web.WebTemplate == "SRCHCENTERLITE" || web.WebTemplate == "SRCHCEN" || web.WebTemplate == "SRCHCENTERFAST")
                {
                    if (!String.Equals(web.CustomMasterUrl, customSearchMasterUrl, StringComparison.OrdinalIgnoreCase))
                    {
                        web.CustomMasterUrl = customSearchMasterUrl;
                        web.Update();
                    }
                }
                else
                {
                    web.AllProperties["__InheritsCustomMasterUrl"] = "True";
                    web.Update();
                }
            }               
        }
    }
share|improve this question
1  
Here is the answer: sharepoint.stackexchange.com/questions/8151/… –  Dmitry Kozlov Oct 10 '12 at 15:14

3 Answers 3

up vote 2 down vote accepted

You are only setting the property on the root SPWeb object, that property is not accessible or propagated down to sub sites.

If you want to apply your master page you will need to write some code in your feature receiver to enumerate child SPWebs and set the property on each child, don't forget to call update on each SPWeb.

share|improve this answer
    
very thanks. i didnt know that i had to set the property in each web. i will try your suggestion. –  ozdogan Oct 10 '12 at 16:10
    
hi james, i've tried your suggestion but still no success. i've added my code in my first post. –  ozdogan Oct 11 '12 at 6:06

I would suggest simplifying to a level that works, then add your conditional logic in. Something like:

using (SPWeb rootWeb = site.OpenWeb()) 
{    
    string masterUrl = "/_catalogs/masterpage/" + DefaultMasterPage;     
    rootWeb.CustomMasterUrl = masterUrl; 
    rootWeb.MasterUrl = masterUrl; 
    rootWeb.Update();

    foreach(SPWeb child in site.AllWebs) 
    {
        if (child.Url != rootWeb.Url) 
        {
            child.AllProperties["__InheritsCustomMasterUrl"] = true;
            child.AllProperties["__InheritsMasterUrl"] = true;
            child.Update()
        }
        child.Dispose();
    }
}
share|improve this answer
    
I dont want to change system master page, so shouldn't i only use child.AllProperties["__InheritsCustomMasterUrl"] = true; –  ozdogan Oct 11 '12 at 10:28
    
tried that, did not succeed again –  ozdogan Oct 11 '12 at 10:50

On the Site Settings page of the sub-site can you see the "Master Page" option in the "Look and Feel" section?

If not, that means the publishing feature isn't enabled and therefore sub-sites won't inherit the master page.

share|improve this answer
    
thanks for your concern but i think you didnt read my post carefully, i am asking the programmatic way of it. –  ozdogan Oct 10 '12 at 16:08
    
Sorry, perhaps I wasn't clear enough. That feature enables master page inheritance. Without it, you can set the property via c#, but nothing will happen. That sounds like the problem you have now described. Also, please see sharepoint.stackexchange.com/questions/9482/… –  SPDuFeu Oct 11 '12 at 7:30
    
hmm you say that it is impossible to set master page inheritance from c# code ? –  ozdogan Oct 11 '12 at 7:40
    
I'm saying that you need to enable the publishing feature for inheritance to work. You can then set it via c#, PowerShell or the UI. –  SPDuFeu Oct 11 '12 at 9:40
    
Publishing feature is already enabled. –  ozdogan Oct 11 '12 at 10:27

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.