Set custom master page

When using a custom master page with SharePoint you need to set all your sub sites to use the same master page.

Here is a function that will recursively change the master page for all sub sites.
Just call if from you feature receiver passing in the master page, the web you want to start with, and if you want to include sub sites.

/// <summary>
/// Sets the custom master page URL for the site.
/// </summary>
/// <param name=”masterPage”>Master page name.</param>
/// <param name=”web”>Site that you want to set the mater page.</param>
/// <param name=”includeSubSites”>Allows you to set all sub-sites as   well.</param>
public static void SetCustomMasterPage(string masterPage, SPWeb web, bool includeSubSites)
{
web.CustomMasterUrl = GetMasterPageUrl(masterPage);
web.Update();

if (includeSubSites)
{
foreach (SPWeb child in web.Webs)
{
SetCustomMasterPage(masterPage, child, true);
child.Dispose();
}
}
}

About these ads

One thought on “Set custom master page

  1. You are right, but problem is that when we create a new site then this site does not inherties master page setting automatically. There are any way to do it? I have created a custom site difinition it is working fine but when client use OOTB Site template like Record Center or any others then problem is occurred.

    Thanks for your valuable posting…

    Regards,
    Manas Mondal

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s