Search code examples
c#sharepoint-2010master-pages

Refresh masterpage for subsites when applying a new one to root web


Let me expose my question : I have a root site, with a masterpage, and many subsites. Some are using the root site masterpage (by inheriting) and some are not using the root site masterpage.

When I update the root site MP with a feature event receiver like that :

SPWeb w = ((SPSite)properties.Feature.Parent).OpenWeb();
Uri masterUri = new Uri(w.Url + "/_catalogs/masterpage/AdventureWorks.master");
//MasterPage used by publishing pages
w.CustomMasterUrl = masterUri.AbsolutePath;
w.AllowUnsafeUpdates = true;
w.Update();

... the master page is updated for the root site but NOT for the subsites wich inherit from the root site master page ! And when i go to Site Master Page Settings Page for a subsite, the "Inherit site master page from parent of this site" radio button is well checked.

When i apply the new MasterPage from the "Site Master Page Settings" Page I doesn't meet this issue.

For information : My root site in a publidhing site and "SharePoint Server Publishing Infrastructure" and "SharePoint Server Publishing" features are running.

Do I miss something ?


Solution

  • Still no responses after one month :/ So I guess that there is no mechanism to update all masterpages for subsites. So I have update the feature activated event receiver like this :

    using (SPWeb w = ((SPSite)properties.Feature.Parent).OpenWeb())
            {
                Uri masterUri = new Uri(w.Url + "/_catalogs/masterpage/AdventureWorks.master");
                w.CustomMasterUrl = masterUri.AbsolutePath;
                w.AllowUnsafeUpdates = true;
                w.Update();
    
                foreach (SPWeb ww in w.Site.AllWebs)
                {
                    if (!ww.IsRootWeb)
                    {
                        Hashtable hash = ww.AllProperties;
                        if (string.Compare(hash["__InheritsCustomMasterUrl"].ToString(), "True", true) == 0)
                        {
                            ww.CustomMasterUrl = masterUri.AbsolutePath;
                            ww.AllowUnsafeUpdates = true;
                            ww.Update();
                        }
                    }
                }
            }
    

    The goal is to test, foreach subweb, if it inherits masterPage (or not). If it does, we have to update the CustomMasterUrl property.