Thursday, October 16, 2014

Change site master page programmatically in SharePoint

It very simple to change the site's master page to custom master page programmatically in SharePoint 2007 environment. Recently we had released a new custom master pages. The master pages are with left menu place holders and another one is without left menu.

Provided a utility to update all sub sites master page. Find the same source code below.

siteUrl=@"http://kmsnet:2020/";
using (SPSite site =new SPSite(siteUrl))
{
foreach(SPWeb web in site.AllWebs)
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
if(web.CustomMasterUrl.ToLower().Equals(("/_catalogs/masterpage/NoLeftMenu.master").ToLower()))
{
web.CustomMasterUrl = "/_catalogs/masterpage/NewNoLeftMenu.master";
web.Update();
}
if (web.CustomMasterUrl.ToLower().Equals(("/_catalogs/masterpage/LeftMenu.master").ToLower()))
{
web.CustomMasterUrl = "/_catalogs/masterpage/NewLeftMenu.master";
web.Update();
}
});
}
}

No comments:

Post a Comment