What is the difference between CustomMasterUrl and MasterUrl?

SharePoint is a content management system targeted towards end users, hence it is supposed to provide maximum functionality out of the box without doing much customizations, let alone changes to the code. So, changing master pages is one of the common requirements by end users, and of course they need it done without changing the code for the page. For end users, they should just select a different master page from a list and it should render for their site.

So, to achieve such an architecture SharePoint uses tokens. In SharePoint, master pages are specified in Content Pages (in Non Publishing Sites) and Page Layouts (in Publishing Sites). These pages use tokens to specify a master page in SharePoint. These tokens will be replaced by the actual master page reference.

The different types of tokens that are there are as follows:
  • Static Tokens
    Static tokens will point to the exact location of the master page.
    Example:
    If our content page is located at "http://MySitecol/MySubSite1/MySubsite2/default.aspx" and we have the master page as "~sitecollection/_catalogs/masterpage/MyMasterPage.master" then the master would be read from "http://MySiteCol/MyMasterPage.master".
    If we have set the master page as "~site/_catalogs/masterpage/MyMasterPage.master" then the master page would be read from "http://MySiteCol/MySubSite1/MySubSite2/MyMasterPage.master".

  • Dynamic Tokens
    Dynamic tokens will not point to the exact location of the master page, but it will point to a place from where the exact location can be retrieved at run time. This place is a property named "MasterUrl" or "CustomMasterUrl".
    Example:
    <%@ Page MasterPageFile="~masterurl\default.master"%>
    <%@ Page MasterPageFile="~masterurl\custom.master"%>

The master page used by the publishing pages within the site use the "Site Master Page" which can be accessed as SPWeb.CustomerMasterUrl.
e.g. SPContext.Current.Web.CustomMasterUrl = "/_catalogs/masterpage/MyCustomMaster.master";
To set the page to use the custom master then we need to set as MasterPageFile="~masterurl/custom.master"

Whereas the master page used by all the forms and view use the "System Master Page" which can be accessed using SPWeb.MasterUrl.
e.g. SPContext.Current.Web.MasterUrl = "/_catalogs/masterpage/MyMasterPage.master";
To the set a page to use the default master then we need to set as MasterPageFile="~masterurl/default.master"

Comments

Popular posts from this blog

What is the difference between a Page Layout and Master Page in SharePoint?

Accessing data from SharePoint 2010 to an ASP.NET application

What is the difference between DDL, DML and DCL?