Dynamically changing master pages in SharePoint Server 2007

Summary: Learn how to dynamically change master pages in a SharePoint publishing site on the fly.

Introduction to Master Pages in SharePoint Server 2007

Microsoft Office SharePoint Server 2007 provides flexible support for publishing sites and features for creating master pages, content types and content pages using object model and declarative syntax. Many organizations require the flexibility to change the master page in same site based on the profile of the user who is connecting. Out of the box, SharePoint provides facility to specify a single default master page for the site which is used by all the page layouts.

This article will provide guidance around how to change the master page for each content page based on target master page specified in the content page. This will allow end users to select the master page while making the pages. Using similar concept users can implement logic for changing master page on each request.

Steps for implementing

  • Create a Publishing Site

  • Create Site Column This site column will be used to store the name of the master page which needs to be applied to the appropriate page. This will be of type choice and users will select the master page while creating a new page.

  • Create Content Type This will be used to create the layout pages. The content type will contain the site column created in previous step.

  • Create Page layout base class The class coded in .NET C# language will implement the logic to change the master page in OnInit event of the PublishingPageLayout class

  • Create Page Layouts derived from Page Layout base class Actual Page layouts on which the content pages will be created

  • Creating pages based on the page layout In this step actual pages will be created and  master pages specified for them

To create a new Publishing Site

1. Using SharePoint 3.0 Central Administration, click Application Management.

2. In the SharePoint Web Application Management section, choose Create or extend Web application.

3. On the Create or extended Web application page, click Create a new web application.

4. Enter the configuration options that you want for IIS Web Site, Security Configuration, Load Balanced URL, Application Pool, and Database Name and Authentication.

5. Click OK, and then wait while the new Web application is created.

6. On the Application Created page, click Create Site Collection.

7. On the Create Site Collection page, type a Name and Description and select the root path (/).

8. Within the Template Selection section, select the Publishing Portal template from the Publishing tab.

9. Enter a Site Collection Administrator, and then click OK to create the site collection.

To create a Site Column

1.     Navigate to the site collection created above. Choose Site Actions dropdown, select Site Settings and from the fly-out select Modify All Site Settings

2.     In Site Settings page, Select Site columns under the section Galleries

3.     In Site Column Gallery page, select the Create option from toolbar

4.     Create a site column with name MasterPageName and of type choice. Provide following choices in the dropdown:

a.     BlueBand.master

b.     BlackBand.master

Users will be able to choose out of these two master pages while creating the site.

5.     Use OK button to create the site column

To create a Site Content Type

1.     Navigate to the site collection created above. Choose Site Actions dropdown, select Site Settings and from the fly-out select Modify All Site Settings

2.     In Site Settings page, Select Site content types under the section Galleries

3.     In Site Content Type Gallery page, select the Create option from toolbar

4.     Create a site content type with name CustomMasterPageContentType. In Select parent content type from: dropdown, choose Publishing Content Types. In Parent Content Type: dropdown, choose Page Layout

5.     Use OK button to create the site content type

Add Site column to Content Type

1. In the Site Content Type information of CustomMasterPageContentType, choose the option Add from existing site columns

2. Select MasterPageName from Available columns and add to Columns to add

3. Select OK to add the column

Create Page Layout base class

1. Create a class library project in Visual Studio with name DynamicMasterPageLayout.

2. Create a class with name BasePageLayout and copy the following code.

namespace DynamicMasterPageLayout


    using System;

    using Microsoft.SharePoint;

    using Microsoft.SharePoint.Publishing;


    /// <summary>

    /// Custom Page Layout class from which all the page layouts should derive.

    /// This class will take care of changing the masterpage. The master page name

    /// is read from the site column which is used to create the site content type.

    /// The site content type is used to create the page layout.

    /// </summary>

    public class BasePageLayout : PublishingLayoutPage


        /// <summary>

        /// Name of the site column which contains the master page name

        /// </summary>

        private const string MasterPageSiteColumn = "MasterPageName";


        /// <summary>

        /// master page location directory

        /// </summary>

        private const string MasterPageDirectory = "/_catalogs/masterpage/";


        /// <summary>

        /// In PreInit, get the context of the page which is getting rendered,

        /// containing the master page name and set that as the master page.

       /// read the column

        /// </summary>

        /// <param name="e"></param>

        protected override void OnPreInit(EventArgs e)




            SPContext current = SPContext.Current;

            if (current != null)


                if (current.ListItem != null &&

current.ListItem[MasterPageSiteColumn] != null)


this.MasterPageFile = MasterPageDirectory +







The OnPreInit method has been overridden to change the master page. This gives a chance to set the custom master page which overrides the setting set in Master Page settings of the site.

3. The SPContext.Current.ListItem gives access to the instance of the publishing content page which is based on this Page Layout. The master page column value is read to find the value of Master Page selected for this page. This page is then set as the master page for this request.

Compile and copy the dll into the bin directory of the website hosting the site collection, path for which should be of format c:/InetPub/wwwroot/wss/<website name>/bin

Add the following safe control entry to the web.config of the website. It will treat the custom class as safe control.

<SafeControl Assembly="DynamicMasterPageLayout, Version=, Culture=neutral" Namespace="DynamicMasterPageLayout" TypeName="BasePageLayout" Safe="True"  />

In this sample, extra privileges will be required to change the master page; you should change the trust level to full in web.config

<trust level="Full" originUrl="" />

Create Page Layouts derived from Page Layout base class

1.  Use SharePoint Designer/ Visual Studio to create the Page Layout with name CustomMasterPageLayout.aspx

2.  Copy the following code in the file

<%@ Page language="C#"   Inherits="DynamicMasterPageLayout.BasePageLayout" %>

<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">

      <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>


<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">

<SharePointWebControls:DropDownChoiceField FieldName="MasterPageName" runat="server"></SharePointWebControls:DropDownChoiceField>


3.  Notice the Inherits from attribute in the code. The page layout now derives from our custom page layouts base class instead of default PublishingLayoutBase class.

4.  The MasterPagename column has been dropped on the page, which will let user select the master page while creating the actual page. It's not hidden and will show on actual page when user will view this page. But this can be configured so that it's not shown to end users and only shown in edit mode.

5.  In Site Settings page, Select Master pages and page layouts under the section Galleries

6.  Use the Upload option to upload the page layout. Associate it to CustomMasterPageContentType as shown below.

7.  Check-in and publish the page so that it can be used by end users to create new pages.

Create Content Pages based on Page Layout

1. Choose Site Actions dropdown, select Create Page

2. In Title specify the name as BlueBand. This page will use the blueband.master page as master page.

3. Select CustomMasterPageLayout.aspx as the page layout.

4. Select Create button to create the page

5. In the page which got create in previous step, from the master page dropdown field, choose the option BlueBand.master

6. Submit the page for approval, approve and page is ready for viewing.

Repeat step 1-5 above to create another page which will be using the BlackBand.master as the master page. In step 2, replace BlackBand as page title and in step 4, choose the option BlackBand.master

Test the solution

1. Navigate to the page Pages/Blueband.aspx. You will observe blueband master page styles applied to the page

2. Navigate to the page Pages/Blackband.aspx. You will observe blackband master page styles applied to the page


SharePoint Server publishing provides great flexibility and extensible points for end users to extend the out of box features provided by product and implement different master pages for different pages in the same site based on the requested page. Many more such scenarios can be realized based on user profile to change the UI dynamically.

Comments (1)

  1. Vaibhav says:

    Good Job buddy!!…i implemented it successfully on my website…

    though you should have mentioned the procedure to add reference of the Microsoft.Sharepoint.Publishing;

    i found it after a lot of googling…and can be found at the location c:program filesmicrosoft office servers12bin

    i hope i will help many people facing the issue…:)

Skip to main content