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
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.
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:
Users will be able to choose out of these two master pages while creating the site.
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
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
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
1. Create a class library project in Visual Studio with name DynamicMasterPageLayout.
2. Create a class with name BasePageLayout and copy the following code.
/// 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.
public class BasePageLayout : PublishingLayoutPage
/// Name of the site column which contains the master page name
private const string MasterPageSiteColumn = "MasterPageName";
/// master page location directory
private const string MasterPageDirectory = "/_catalogs/masterpage/";
/// 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
/// <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=188.8.131.52, 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=184.108.40.206, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=220.127.116.11, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=18.104.22.168, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=22.214.171.124, 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.
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 files\microsoft office servers\12\bin
i hope i will help many people facing the issue...:)