How to use the site’s masterpage from a _layout SharePoint page

layoutmaster-pagessharepoint

I want to use a masterpagefile (the site's one if this makes sense) in my SharePoint 12 hive/template/layouts/TelephoneBookList/test.aspx file. However, whenever I add MasterPageFile="~/_layouts/simple.master" or something like that to my test.aspx page, I get a sharepoint error page and the following in the log file:

01/12/2009 10:56:26.95 w3wp.exe (0x1064) 0x1280 Windows SharePoint Services Topology 0 Critical Invalid URL: http://localhost. You may also need to update any alternate access mappings referring to http://mcdevsp. Help on this error: http://go.microsoft.com/fwlink/?LinkId=114854

01/12/2009 10:56:27.09 w3wp.exe (0x1064) 0x1280 Windows SharePoint Services General 8dzz High Exception Type: System.Web.HttpException Exception Message: The file '/_layouts/TelephonebookList/~masterurl/default.master' does not exist.

01/12/2009 10:56:27.23 w3wp.exe (0x1064) 0x1280 Windows SharePoint Services Topology 0 Critical Invalid URL: http://localhost. You may also need to update any alternate access mappings referring to http://mcdevsp. Help on this error: http://go.microsoft.com/fwlink/?LinkId=114854

01/12/2009 10:56:27.70 w3wp.exe (0x1064) 0x1280 Windows SharePoint Services General 8dzw Medium spHttpHandler:GetHash started

01/12/2009 10:56:27.70 w3wp.exe (0x1064) 0x1280 Windows SharePoint Services General 8dzx Medium spHttpHandler:GetHash finished

Without the MasterPageFile attributes, it works perfectly, however it does not have the global Look & Feel of the web site.

Many thanks.

Best Answer

Try this:

Leave the MasterPageFile declaration at it's default (~/_layouts/simple.master) and override OnPreInit in your custom code.

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    this.MasterPageFile = SPContext.Current.Web.MasterUrl;
}