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'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 ..)

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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);
publishingWeb.Update();
share|improve this answer
    
thats the kind of simple anwser I was looking for. Thanks! –  user1007 Jan 3 '11 at 8:03
    
You're welcome :) –  Waldek Mastykarz Jan 5 '11 at 6:19
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
add comment

http://msdn.microsoft.com/en-us/library/gg447066(v=office.14).aspx

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

share|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
add comment

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";
  site.Update();
  site.Dispose();
}
share|improve this answer
add comment
private void SetMaster(SPWeb web, string masterpagePath, string custommasterpagePath) 
{
    web.CustomMasterUrl = masterpagePath;
    web.MasterUrl = custommasterpagePath;
    web.Update();

    foreach(SPWeb child in web.Webs) 
    {
        try {
            SetMaster(child, masterpage, custommasterpage);
        } 
        finally
        {
            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.

share|improve this answer
add comment

Your Answer

 
discard

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