Random Technical Thoughts

December 18, 2009

Set custom master page

Filed under: SharePoint — Tags: , — chrisbarba @ 4:36 pm

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

1 Comment »

  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

    Comment by Manas Mondal — May 6, 2010 @ 2:29 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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 )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: