Assign a Unique Master Page to a Page Layout in a Publishing Portal

The scenario goes, you have a SharePoint 2010 Publishing site and you have created a custom master page that is set as the “Custom” master page for the site. Now all of your pages will use this assigned custom master page as expected. But let’s say that you create a new custom page layout that only special pages will use, and this page layout should use yet a different master page. You go ahead and update the Page directive on the Page Layout by adding something similar to:

1
MasterPageFile="/_catalogs/masterpage/nightandday.master"

But after saving, checking in and publishing, you find that the pages using this page layout still use the site’s custom master page, not the assigned master page for this particular page layout. Why?
Well this is because Page Layout pages inherit from

1
Microsoft.SharePoint.Publishing.PublishingLayoutPage

and this class does not allow you to modify the masterpagefile property in the Page Layout. The PublishingLayoutPage class automatically sets the master page to the site’s custom master page during OnPreInit!
Microsoft’s class definition for the PublichingLayoutPage can be found here, be sure to review the Remarks as well.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishinglayoutpage.aspx
What is your workaround? Well unfortunately you need to create your own custom class that will inherit from the PublishingLayoutPage class, and then override the CustomMasterUrl property with the master page you would like this custom PublishingLayoutPage class to use.  This isn’t too difficult to do, although you would need to have some coding experience and Visual Studio 2010.
Do you need help creating this custom class? No problem, a great blog article was written for MOSS / SharePoint 2007 that you may find helpful. The process to create this custom class is almost exactly the same in 2007 and 2010.
http://blogs.msdn.com/b/jannemattila/archive/2007/04/14/adding-functionalities-to-pages-by-inheriting-publishingpagelayout.aspx
There is another potential work around that I will discuss in another post. The next post is on the difference between the PublishingLayoutPage class and the WebPartPage class, and how they can be used to create a “custom” page layout that allows for a custom master page applied to only a particular set of page layouts.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishinglayoutpage.aspx

4 thoughts on “Assign a Unique Master Page to a Page Layout in a Publishing Portal

  1. Juan Larios August 4, 2011

    you don’t override the CustomMasterUrl, in the code extracted with Reflector it’s really the MasterPageFile that is overriden by the web.CustomMasterUrl. What is really happening here is the OnPreInit of the page is getting the master page that is set at the web level. This is how all pages have the same look and feel as the parent site. If you want to have a custom master page set on a page layout, then you override the the OnPreInit and set the MasterPageFile by your own custom url.
    I think you were communicating this, it just wasn’t clear to me when I first read this. I have posted the Reflector code on my blog and referenced this blog post for background info. Thanks for this post!
    http://geekswithblogs.net/juanlarios/archive/2011/08/04/sharepoint-2010-assign-a-unique-masterpage-to-a-page-layout.aspx

  2. Hamzeh Al-Bakri February 7, 2012

    This post was very helpful to understand the issue thax for you ,,
    what i did is another workaround for this issue ,,
    1 – de-attach the page from its layout page
    2 – remove the publishing feature registry in top of the page and replace it with
    webpart pages registry reference
    [Inherits=”Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c” ]

    1. Eric Overfield February 7, 2012

      A very clean way to describe a great solution. I wrote another blog post on this as well and described some of the ramifications. I like your quick and direct explaination though.

  3. John August 17, 2012

    Helpful info. Too bad Microsoft doesn’t make this a little easier…

Leave a Comment

Your email address will not be published. Required fields are marked *

Enter Code *

Filed Under

Related

Do you need to build a custom search-driven component for your #digitalworkspace while saving thousands of hours of development time?

Join us on as we lift the hood of #PnP Modern Search v4, show you how it's built, & take you on a live coding adventure.

http://ow.ly/MTnL50EpN5J

#Hybridwork is more than just a buzzword to latch onto; it’s the future for many organizations for the unforeseeable future. So, how can you use #Microsoft365 for #hybridwork? Let's breaks it down…

http://ow.ly/ay2e50EmNhg

It's that time again! Join the #PixelMillWebinars on 4/29 where @EricOverfield lifts the hood of #PnP Modern Search v4, shows how it's built, & takes you on a live coding adventure to customizing your own web part using the power of the existing codebase.

http://ow.ly/EI5h50EmNqj

What? Anchor links now work in #SharePoint site navigation! This fix snuck in and I completely missed it. Use them to create a TOC for long pages or cross link between pages. They work in text or quick links web parts & nav too! https://support.microsoft.com/en-us/office/create-and-use-modern-pages-on-a-sharepoint-site-b3d46deb-27a6-4b1e-87b8-df851e503dec#bkmk_pageanchors

A big thank you to David Leveille and @CrushNetworks for having our President + Co-Founder @EricOverfield on this episode of #TheVirtualWaterCooler!

Subscribe to PixelMill's
E-news

* indicates required

Let's Talk Digital
Workspaces Today

Get In Touch