AAron nAAs

All MindsharpBlogs

AAron's SharePoint notepad

My Links

Archives

Blog Stats

My Sites

Change MasterPageFile for a specific PageLayout

I wanted ONE page in my SharePoint (WSS v3) site to NOT have the site's normal MasterPage branding.

Ends up that if you want to change the Master Page for one Page Layout, you need to do something special. Normally each site has a master page and an alternate master page assigned for the site, and SharePoint sets the MasterPageFile for you behind the scenes. The Microft.SharePoint.Publishing.PublishingLayoutPage class is what changes the MasterPageFile to the one chosen for the site. Normally your PageLayout files begin with this line:

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

If you want a different master page, you may try this (which won't work):

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

Since the PublishingLayoutPage changes the MasterPageFile attribute during OnPreInit(), your MasterPageFile will be set (via the attribute in the tag), and compiled (and even error if not correct), but then it will be completely ignored during rendering. This happens because PublishingLayoutPage changed the master page after you set it (always overriding the tag's attribute).

To set the MasterPageFile, you need to “override the override” by starting your PageLayout with your own OnPreInit() handler:

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

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

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

<%@ Register Tagprefix="OSRVWC" Namespace="Microsoft.Office.Server.WebControls" Assembly="Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

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

<%@ Register Tagprefix="SEARCHWC" Namespace="Microsoft.Office.Server.Search.WebControls" Assembly="Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

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

<script runat="server">

protected override void OnPreInit(EventArgs e)

{

    base.OnPreInit(e);

    this.MasterPageFile = "SomeOther.master";

}

</script>

posted on Tuesday, February 26, 2008 1:54 PM

Feedback

# re: Change MasterPageFile for a specific PageLayout 2/28/2008 8:53 AM Brian Bedard

This is great except I thought SharePoint disallowed the use of server-side script in an aspx page?

# re: Change MasterPageFile for a specific PageLayout 2/28/2008 12:55 PM AAron nAAs

As long as the page is uncustomized (ghosted) living on the filesystem, you can put server-side scripts into .aspx files. I just ran a test, and downloaded the page layout file, and reuploaded it (unedited). The page is then customized (unghosted) and lives in the database. The page now produces the error:
Code blocks are not allowed in this file.

Title  
Name  
Url
CAPTCHA
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments