Saturday, October 1, 2011

SharePoint 2010 - How to specify a different master page for a publishing page layout


How to associate a different master page than the default master page for a publishing page layout in SharePoint 2010?


There may be scenarios when certain pages in a SharePoint site require associations with a different master page(s)..

Typical examples could be pages that are required to be loaded inside SharePoint dialog boxes. These type of pages show up fine with minimal.master master page associated with them.


Applying “minimal.master” master page to a page layout "CustomDialogPages.aspx" did not work.

When the page layout inherits from the 'PublishingLayoutPage' class the master page gets set on PreInit(), that's why it ignores what we put in the page directive.

There are 2 possible ways to get this to work:

1.) Add the below inline code to your page layout. You can either use SharePoint Designer or download the page layout from page layouts gallary and update it, then upload it. (Recommended if you are not a developer or if you don't want to write code)
<script runat="server" type="text/C#">
protected override void OnPreInit(EventArgs e)
                this.MasterPageFile = "/_catalogs/masterpage/minimal.master";
As SharePoint does not allow inline scripts, it throws error “Code Block are not allowed in this file“
To get rid of this error, the page layouts that use inline code will need to be included in the pageparserpaths node in web.configs in all WFEs as shown below:
                <PageParserPath VirtualPath="/_catalogs/masterpage/CustomDialogPages.aspx" CompilationMode="Always" AllowServerSideScript="true" /> 

2.) Create a custom base class that inherits from the 'PublishingLayoutPage' class and override it's PreInit() method Then just change the layout to inherit from that class instead.
    /// <summary>
    /// The base class which all of our dialogues will use.
    /// </summary>
    public class MinimalPublishingLayoutPage : PublishingLayoutPage
        protected override void OnPreInit(EventArgs e)
            this.MasterPageFile = "/_catalogs/masterpage/minimal.master";
Associate the new class with required page layout(s)

Change the "Inherits" property in the below tag:
<%@ Page language="C#"   Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
<%@ Page language="C#"   Inherits="Namespace.MinimalPublishingLayoutPage, Assembly, Version=Version Number, Culture=neutral, PublicKeyToken=Token" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>

Now any pages that use page layouts that are inherited from our custom pagelayout class will use minimal.master master page.

No comments:

Post a Comment