I'm creating a site feature reciever which sets a custom master page on the root web. I would also like the master page set on the root to be inherit to all the subsites. What is the most elegant way to ensure this (using .NET)?

(Obviously I could iterate through all subsites and set it manually ..)


There an easier way to do this using the SharePoint API:

SPWeb web = SPContext.Current.Web;
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
publishingWeb.CustomMasterUrl = "/_catalogs/masterpage/mycustom.master";
publishingWeb.CustomMasterUrl.SetInherit(true, true);
| improve this answer | |
  • thats the kind of simple anwser I was looking for. Thanks! – user1007 Jan 3 '11 at 8:03
  • 1
    Old answer I know, but I would like to point out for future observers that this doesnt actually work. The CustomMasterUrl property of the PublishingWeb class is readonly. – John S Feb 6 '13 at 5:41
  • An alternative way would be to use the publishingWeb.CustomMasterUrl.SetValue(string, bool) method to set the value and force the subwebs to inherit the Master Page. – Waldek Mastykarz - MVP Jul 19 '14 at 6:12
  • how can this be done in CSOM? web.CustomMasterUrl is a string there – Vignesh Subramanian Oct 7 '15 at 3:44
  • 1
    @WaldekMastykarz is there a equivalent for CustomMasterUrl.SetInherit in CSOM? – Vignesh Subramanian Oct 8 '15 at 4:46

I think the best we can do is something like the following in the FeatureActivated method.

foreach (SPWeb site in siteCollection.AllWebs) 
  site.MasterUrl = "/_catalogs/masterpage/mycustom.master";
  site.CustomMasterUrl ="/_catalogs/masterpage/mycustom.master";
| improve this answer | |
private void SetMaster(SPWeb web, string masterpagePath, string custommasterpagePath) 
    web.CustomMasterUrl = masterpagePath;
    web.MasterUrl = custommasterpagePath;

    foreach(SPWeb child in web.Webs) 
        try {
            SetMaster(child, masterpage, custommasterpage);
            if(child != null) child.Dispose();

Call this passing the SPSite.RootWeb, and specifying the path to your master pages. It'll recurse over all the sites below that rootweb in the site collection (i.e. all of them!)

Note that you may need to consider whether the sites below use different master pages as this can cause problems, particularly with Meeting Workspaces, but possibly others.

You may also need to consider storing the original master pages details in the property bag for the web so you can restore them later (e.g. feature deactivation)

And you may wish to make sure that themes are turned off and don't interfere with your look and feel.

| improve this answer | |


This will provision the childsites by event receiver by taking toplevel site masterpage...

| improve this answer | |
  • While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. – SPDoctor May 2 '13 at 6:24
  • Sure.Will tk care henceforth of the guidelines. – Krutika Jun 21 '13 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy