How SharePoint Pages are associated with a Master page

So, yesterday’s discussion of the problems with master pages for Publishing and Meeting sites raised a bit of a question with my colleagues. If a site (SPWeb object in the code) has both MasterUrl and CustomMasterUrl properties, how does a page ‘know’ which one to use? Why do publishing pages use the value in CustomMasterUrl, and other pages use MasterUrl?

Well, to find the answer to that, you’ve got to look in the code of the .aspx content page. Here’s the top of one in SharePoint Designer:

Yup, there is a MasterPageFile as per standard ASP.NET. However, the value of this isn’t normal – it’s not the URL itself, but rather a token that gets replaced at runtime.

  • ~masterurl/default.master is resolved to the value of the MasterUrl property (i.e. the ‘System Master Page’ on a publishing site).
  • ~masterurl/custom.master is resolved to the value of the CustomMasterUrl property (i.e. the ‘Site Master Page’ on a publishing site).

If you look in, say, the default.aspx page on a team site, you’ll see that it uses ~masterurl/default.master.

If you look in, say, the default.aspx page on a meeting workspace, youll see that it uses ~masterurl/custom.master.

If you look in publishing pages, though, you have to look at the page layouts (which are the actual .aspx pages being used). These don’t have a have a MasterPageFile defined – what gives? Well, they inherit from the Microsoft.SharePoint.Publishing.PublishingLayoutPage class, which sets the master page file internally during OnPreInit! You can see in Reflector that it’s using CustomMasterUrl though.

Advertisement
How SharePoint Pages are associated with a Master page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.