Thursday, December 17, 2009

PublishingLayoutPage,WebPartPage and custom Master pages

Page Layouts form an essential part of any Web Content Management (WCM) site built on MOSS 2007.
Page layouts when combined with master pages; define the layout of the page rendered. They also define the editable regions of the page, usually implemented by web parts and Meta data (Field controls) associated with the page.

Let us consider the base class from which the page layouts in any publishing site inherit from, the
Microsoft.SharePoint.Publishing.PublishingLayoutPage class. If we see any of the out of the box page layouts that SharePoint provides, we can see that they all inherit from the PublishingLayoutPage class.
Going a bit deeper into the implementation of the PublishingLayoutPage class, the documentation on msdn indicates the inheritance hierarchy of the class as follows:


System.Object
System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.Page
Microsoft.SharePoint.WebPartPages.WebPartPage
Microsoft.SharePoint.Publishing.PublishingCachablePage
Microsoft.SharePoint.Publishing.PublishingLayoutPage


So there is the PublishingCachablePage that separates the Microsoft.SharePoint.WebPartPages.WebPartPage which is the class all the pages in WSS derive from and the PublishingLayoutPage.

As we know if we derive out page layout from the PublishingLayoutPage, the master page for the layout is taken from the Site Master Page settings of the current site; this is an implementation of the PublishingLayoutPage class, using a reflector:


[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public class PublishingLayoutPage : PublishingCachablePage
{
// Methods
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPContext current = SPContext.Current;
if (current == null)
{
SPException exception = new SPException(Resources.GetString("ErrorInvalidSPContextCurrent"));
ULS.SendTraceTag(ULSTagID.tag_7ob3, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Unexpected, "PublishingLayoutPage.OnPreInit(): Exception: SPContext.Current is null");
throw exception;
}
SPWeb web = current.Web;
this.MasterPageFile = web.CustomMasterUrl;
}
}


If you want to add new functionality to your publishing page layouts you can extend the PublishingLayoutPage class. This is a good post that describes how this can be achieved:


If you want to specify a custom master page for more than one page layout to be used in a specific site you can override the OnPreInit method of the PublishingLayoutPage class and set the this.MasterPageFile property appropriately.