Saturday, October 1, 2011

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

What?

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

Why?

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.

How?

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)
{
                base.OnPreInit(e);
                this.MasterPageFile = "/_catalogs/masterpage/minimal.master";
}
</script>
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:
      <PageParserPaths>
                <PageParserPath VirtualPath="/_catalogs/masterpage/CustomDialogPages.aspx" CompilationMode="Always" AllowServerSideScript="true" /> 
      </PageParserPaths>

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)
        {
            base.OnPreInit(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=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
to
<%@ 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