MOSS 2007 – Specifying Custom Master Page for the Specific Page Layouts

If you are working on the publishing portal, changing custom master pages for all the publishing pages is very easy through the browser interface. Simply change the site master page settings to the custom master page and it will apply to all the content pages based on the OOB or custom page layouts.

This is great if you have only one single custom master page and you want to apply to the publishing site. Howerver, in real world, more than often, you will have more than one custom master page to apply to the specific pages based on the specific page layouts. e.g. custom master page showing corporate branding on the publishing site home page, custom master page showing individual department or product branding etc.

If you are creating page layouts using Visual Studio and deploying them as a feature, Normally your custom page layouts are derived from the PublishingLayoutPage class and begins with this line.

   1:  <%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

To change the custom master page for the individual custom page layouts, you might try to specify the MasterPageFile property in the page directive like ASP.NET as follows.

   1:  <%@ Page language="C#" MasterPageFile ="customIntranet.master" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

Unfortunately this will never apply the custom master page to the content pages based on the specific page layouts in the SharePoint. The reason for this is – Page layouts are based on the PublishingLayoutPage class and PublishingLayoutPage class overrides the MasterPageFile attribute during OnPreInit() event handler to override the default master page with the site master pages settings in the UI. Because of this, even though your master page is set through the MasterPageFile property, it will immediately overwritten in the Pre-Init event of the publishing pages base class and completely ignored during rendering.

To set the custom master page for specific page layout, you need to override the default behaviour by setting the MasterPageFile property in the the Page Layout’s OnPreInit() handler. First step is to make sure you have code behind page for the custom page layout. If you don’t have code behind class, please check out the Andrew Connell’s tutorial on MSDN to understand how to create a code-behind class for a publishing page layouts – http://msdn.microsoft.com/en-us/library/bb986729.aspx.

Once you have the code-behind class, override the OnPreInit event method and set the MasterPageFile property immediately after the base.OnPreInit(e) call.

   1:  protected override void OnPreInit(EventArgs e)
   2:  {
   3:      base.OnPreInit(e);
   4:      this.MasterPageFile = "intranetCustom.master";
   5:  }

Thanks to the following links for wonderful tip.
http://sharepoint.coultress.com/2008/07/setting-master-page-for-individual-page.html
http://mindsharpblogs.com/Aaron/archive/2008/02/26/4379.aspx

If you are reading second link above, please notice that author is specifying the custom master page using the inline script.

   1:  <script runat="server">
   2:  protected override void OnPreInit(EventArgs e)
   3:  {
   4:     base.OnPreInit(e);
   5:     this.MasterPageFile = "SomeOther.master";
   6:  }
   7:  </script>

Please remember that inline server side scripting in .aspx files is only supported for the uncustomized page layouts living in the file system. As long as your page layouts are not customized using the SharePoint Designer or Browser Interface, the content pages based on your page layouts will work fine. As soon as they are customized and live in the database, users will get the error “Code blocks are not allowed in this file”. It is best practice to specify the custom master page through the code behind instead of the inline scripting to avoid future errors due to page layout customizations.

Advertisements
This entry was posted in SP2007 DEV. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

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