Pergunta

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

Foi útil?

Solução

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();

Outras dicas

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();
}
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.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top