Wednesday, June 3, 2009

Programmatically Inherit Master Page in MOSS 2007

Make a instance of SPSite ospsite and use the below for Programmatically Inherit Master Page master page to all web in site collection

using(SPWeb web in ospsite.AllWeb())
{
web.MasterUrl = web.Site.RootWeb.MasterUrl;
web.CustomMasterUrl = web.Site.RootWeb.CustomMasterUrl;
web.AlternateCssUrl = web.Site.RootWeb.AlternateCssUrl;
web.Update();
}

3 comments:

  1. This post is what i've been looking for.
    Do this works in Wss 3.0 also and where we need to place this code..in a feature?
    Please clarify.

    ReplyDelete
  2. Note that when using the AllWeb attribute, you should dispose of the webs after the update to prevent memory leaks.
    Web.Dispose();

    ReplyDelete
  3. Hey, MOSS seems to function not that well when I use Vista in my laptop, but in XP Pro (PC) it works just fine. Can anyone figure this out for me?

    ReplyDelete