Philippe
Philippe

Reputation: 21

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 ?

Upvotes: 1

Views: 3260

Answers (2)

gdbdable
gdbdable

Reputation: 4501

Use this to set masterpage on root and all subwebs:

        var web = site.RootWeb;

        web.MasterUrl = web.CustomMasterUrl = SPUtility.ConcatUrls(web.ServerRelativeUrl, "_catalogs/mymaster.master");
        web.Update();

        foreach (SPWeb subWeb in site.AllWebs)
        {
            using (subWeb)
            {
                if (subWeb.IsRootWeb) continue;

                var hash = subWeb.AllProperties;

                subWeb.MasterUrl = subWeb.CustomMasterUrl = web.MasterUrl;

                hash["__InheritsMasterUrl"] = "True";
                hash["__InheritsCustomMasterUrl"] = "True";

                subWeb.Update();
            }
        }

Upvotes: 0

Philippe
Philippe

Reputation: 21

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.

Upvotes: 1

Related Questions