Tuesday, February 23, 2010

How to apply Custom master page on specific page of the site?

Recently we face a situation where on the publishing site, we had two master pages. One for home page with some jazzy look and other pages with simpler branding.

Simple solution could be creating new Page Layouts, referring another master page.

Here are the simpler steps to achieve this.

  1. Create a new Page Layout (either by using SharePoint designer or manually creating new aspx page)
  2. Specify custom.master as the MasterPageFile attribute for the page directive.

MasterPageFile="~masterurl/custom.master"

  1. On PreInit event, change the MasterPageFile url to required master page.
<script runat="server">

void Page_PreInit(Object sender, EventArgs e)

{

this.MasterPageFile = "~SiteCollection/_catalogs/masterpage/myCustom.master";

}

</script>

  1. This may lead to error with “Code-blocks are not allowed in this file.”

PageParserPaths is a special node in web application's web.config file. By Default this node is empty. You can add nodes to specify the virtual paths where you want to allow server side scripts:

<pageparserpaths>

<pageparserpath virtualpath="sites/mysite/_catalog/masterpage/*" compilationmode="Always" allowserversidescript="true" includesubfolders="true">

</pageparserpaths>