Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal



"Inheriting" the Master Page from the Current Site Context in MOSS 2007

This post originally appeared on my MSDN blog:

Since I no longer work for Microsoft, I have copied it here in case that blog ever goes away.

In my previous post, I showed how you can override the hard-coded "application.master" in Microsoft Office SharePoint Server (MOSS) 2007 application pages (e.g. /Library/_layouts/viewlsts.aspx).

Note that for custom application pages (i.e. those ASP.NET pages that you create to run under the context of a SharePoint site) you don't need a custom HttpHandler in order to "inherit" the master page from the current SPWeb.

All you need to do is simply set the master page during the PreInit phase of the ASP.NET page lifecycle.

This is precisely what I developed my custom SharePointPage base class for:

    /// <summary>
    /// Base class for ASP.NET pages that run under the context of a SharePoint
    /// site (e.g. /en-US/Search/Library/_layouts/PublicationSummary.aspx).
    /// </summary>
    /// <remarks>
    /// Inheriting from this base class ensures that the correct master page is
    /// used (as specified by the current site context).
    /// </remarks>
    public class SharePointPage : Page
        protected override void OnPreInit(
            EventArgs e)


        private void SetMasterPageFromCurrentWeb()
            if (SPContext.Current == null)
                throw new InvalidOperationException(
                    "This page must execute within a SharePoint site"
                    + " (SPContext.Current is null).");

            string masterPageFile = SPContext.Current.Web.CustomMasterUrl;

                "Overriding master page with {0}...",

            this.MasterPageFile = masterPageFile;

To see a real-world example of this in action, simply browse to one of the "publication summary" pages on the Agilent Technologies LSCA site (try searching for 6850, filtering the search results to Library, and then clicking one of the search results). Note that the PublicationSummary page class inherits from the SharePointPage base class.

    public partial class PublicationSummary : SharePointPage,

For the sake of this post, ignore the IView interface. That is used for something else entirely (i.e. a simple Model-View-Controller framework) that perhaps one day I'll get around to covering as well.


No comments posted yet.

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Watermelon

Please add 3 and 6 and type the answer here: