SharePoint 2007: using the masterpage from your site in custom _layouts pages

I got a question from Jeff:

I'm wondering if you have experimented with creating application pages that will both run in the sharepoint context and that can use the default.master of the web. When creating application pages in the _layouts folder these pages can use the application.master, but cannot use the default.master.

I tried to do some theoretical thinking on this topic, but it could be that I'm way off. So here are my thoughts. Please let me know if you tried it out, if you were successful, and what the best solution to this interesting problem is.

Hi Jeff,

If I understand you correctly you want to create application pages running in _layouts that uses the same masterpage as the site in which context the page is running.

First thing is that if you want to use a masterpage from the site context, you need to have the same content placeholders as are expected by the master page.

Master pages can be loaded dynamically. This can be done by assigning a master page file to the MasterPageFile property in the Page object. This property may only be assigned in the Page PreInit event, this is the first event executed page execution lifecycle.

SharePoint has a set of static and dynamic tokens for specifying the masterpage to use:

~masterurl, ~site, and ~sitecollection. I assume you already tried to use ~site, that would be the easiest solution.

Assuming that ~site does not work, one problem is now: how can we access the master page file that is in the site context. I don't know if it works if you specify a path pointing to a file in the site, because we are running in a different virtual directory. Otherwise you could implements a VirtualPathProvider that allows you to access files in the SPWeb that is your current context.

Could be that you first have to assign a dummy masterpage that has all the correct placeholders, and that this masterpage must be stored in the _layouts pages as well.

Anyone?

UPDATE: From the comments, Roni Hofer confirms that he got it working as follows:

protected override void OnPreInit(EventArgs e)

{

 base.OnPreInit(e);

 SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb();

 string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/my.master";

 this.MasterPageFile = strUrl;

}

 

Where "my.master" has to be stored in master page gallery of the site.

Published Tuesday, November 14, 2006 12:37 AM by svdoever
Filed under:

Comments

Tuesday, November 14, 2006 4:46 AM by Roni Hofer

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

The following worked for me:

protected override void OnPreInit(EventArgs e)

{

  base.OnPreInit(e);

  SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb();

  string strUrl = _webCurrent.ServerRelativeUrl + "/_catalogs/masterpage/my.master";

  this.MasterPageFile = strUrl;

}

Where "my.master" has to be stored in master page gallery of the site.

Tuesday, November 14, 2006 4:56 AM by Roni Hofer

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Correct code:

protected override void OnPreInit(EventArgs e)

{

 base.OnPreInit(e);

 SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb();

 string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/my.master";

 this.MasterPageFile = strUrl;

}

Tuesday, November 21, 2006 12:29 PM by Jeff

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Serge,

Thanks for helping out with this and the few that have commented. I've been playing around with several different ideas on how to handle this(Adding the pages as features, the PreInt Override, User Controls on webpart pages...). I've settled on the suggested PreInt method because of the special case of my implementation of MOSS and development needs.

Thanks again,

Jeff

Friday, January 26, 2007 7:23 AM by Raul

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

This seems the be best solution to the problem, but, i don't know how to attach this code to all setting pages, Can you help me?,

Thanks!,

Raul

Wednesday, April 18, 2007 8:39 AM by Rob Pearmain

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Me too, how can I attach this code to all the settings pages,

Thanks

Rob

Wednesday, May 16, 2007 10:38 AM by Pranab Paul

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I have successfully used this code in my application. But it seems not working with Forms Based Authentication (neither string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/default.master"; nor string strUrl = myWeb.ServerRelativeUrl + "/_layouts/application.master";)

I have this entry in web.config file:

<authentication mode="Forms">

<forms loginURL="/_layouts/loginpage/login.aspx" />

</authentication>

and wanted to add master page in the login page. The login page is working fine without master page.

Friday, May 18, 2007 2:25 PM by nn

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I have created a web application under the layouts\Project folder. When I try to change the master page url in the preinit event, I get an error.

The virtual path '/_catalogs/masterpage/default.master' maps to another application, which is not allowed.

Thanks for your response

Friday, June 15, 2007 5:34 PM by Kathy

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I'm with Raul & Rob on this.  How is this code attached to administrative pages ?

Tuesday, July 03, 2007 1:27 PM by Pranab's Blog

# How to implement a Postback Enabled Custom aspx page in Layouts Directory using Masterpage from any site

Here are the steps to follow: 1. Start a new Web Application using Visual Studio 2005 and go to the Property_Pages

Tuesday, July 03, 2007 1:55 PM by Noticias externas

# How to implement a Postback Enabled Custom aspx page in Layouts Directory using Masterpage from any site

Here are the steps to follow: 1. Start a new Web Application using Visual Studio 2005 and go to the Property_Pages

Tuesday, July 31, 2007 10:27 AM by Huzaifa Tapal

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I tried using this approach, however, I keep getting the SharePoint error page with "Unknown Error"

Any ideas?

Sunday, September 16, 2007 5:35 PM by Søren

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Looks great guys, but you really shoult dispose that SPWeb object, otherwise you are doing a memory leak, that might take down the entire site.

Use something like:

protected override void OnPreInit(EventArgs e)

{

base.OnPreInit(e);

using( SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb() ){

string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/my.master";

this.MasterPageFile = strUrl;

}

}

Thursday, September 27, 2007 9:26 AM by juniormoss

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Hi All,

I have a problem with customization of the CreatePage.aspx.

When i try to remove 1 control in the page (of course in the copy) i get an error.

Why is it so difficult to make a custom CreatePage.aspx.

I want also change the buttons, so i have created a .cs file. When i have only a html form with 3 new controls, it works fine, but i need the masterpage / navigation etcetera.

Can anybody help me please?

/juniormoss

Friday, September 28, 2007 6:08 AM by svdoever

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

See also www.thelineberrys.com/.../using-your-sharepoint-sites-master-page-on-all-application-pages-3.html for aan approach to handle this problem, based on my original article.

Sunday, November 04, 2007 1:38 AM by gautham

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

can you provide a sample source code for the aspx page?

Saturday, November 10, 2007 9:28 AM by Firas

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Me too, how can I attach this code to all the settings pages,

Thanks

Monday, November 19, 2007 6:14 PM by Peter Vranich

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Hi,

I have tried to follow this idea and the idea suggested by Steve Lineberry... www.thelineberrys.com/.../using-your-sharepoint-sites-master-page-on-all-application-pages-3.html but on certain pages, _layouts/CheckIn.aspx and _layouts/CstWrkflIP.aspx i get the following error.

Unable to validate data.   at System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength)

  at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)  

This error will not show up unless you enable debug to true in the web.config file and customerrors to off like such.

<compilation batch="false" debug="true">

and

<customErrors mode="Off" />

has anyone else been able to get this to work on all _layouts/*.* pages?

The _layouts/CheckIn.aspx page can be found by trying to ckein a page via the administration pages not SPD and _layouts/CstWrkflIP.aspx can be found by trying to add a workflow, this is the second page of the add workflwo process, approval was the one i was trying to add to the pages library in a site.

Any ideas???

Wednesday, November 21, 2007 11:37 AM by Whatever-ishere

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

thanks for the GREAT post! Very useful...

Friday, December 07, 2007 11:15 AM by Steve Lineberry

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Hey Peter Vranich, I figured out the "Unable to validate data" error.  It's the search box.  Check out my new master page and code behind at:  www.thelineberrys.com/.../using-your-sharepoint-sites-master-page-on-all-application-pages-2.html

Thursday, December 13, 2007 12:37 PM by Eric

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I would like to do a sime thing as explaine dbelow. Can  you please tell me if it is possible in wss v3?

I want to create my own custom mastser page and all it has is the site actions control (so that I can do edit page create page etc ) (and please i dont want to have any other control that comes in the defualt.master). and text that says hello (The reason I am suing jut hello text is because if i can write hello text I can create my own custom controls). Once I cfreate this page I want to be able to put this in _catalogs/master pages/ folder. Then I would like to create a Helloworld.aspx  layout page that uses   the above created master page and I want to enable creating this Helloworld.aspx using a feature in wss v3.

Any ideas? It looks like it is a reasonable thing to do because then I have complete control and versatility on what i can do on the master pages.

Also why is a site definition restricted to one master page. It would be nice to have a built in feature whic allows us to choose master page at the time of creating a page.

Monday, December 17, 2007 5:14 PM by Wes Turner

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I have a quickie way that worked for me with no code necessary.  If your layouts page has no master associated with it at all, you can simply use the Page Viewer Web Part to display the _layouts page in any SharePoint page.

Saturday, February 09, 2008 12:16 PM by Deborah French

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

I get an unknown error when I use forns authentication to active directory - the default site is set to windows.   This was working for a while , but now when I select the extranet site I get unknown error instead of the login box - any suggestions

Monday, October 27, 2008 11:29 AM by Andrew Kinnear

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

According to MS you are not supposed to change the application pages or the application master page, you loose MS support should you do this. If you want to change the master page you can only do it the way this article describes. But then you would have to add the code to every application page. Not practical.

If you want to apply this to ALL application pages (which makes sense, I mean why do this any other way) then make a duplicate of the layouts folder, and change the IIS Virtual directory for _layouts to point to the new directory, then you can change the application.master page to your heart’s content.

Monday, November 10, 2008 11:26 AM by Carsten Keutmann

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

This works for me, plain and simple:

protected override void OnPreInit(EventArgs e)

{

 base.OnPreInit(e);

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

}

Wednesday, December 03, 2008 11:18 PM by chintan

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

very good post , very good idea to add master page from code...

Wednesday, December 10, 2008 2:51 AM by Bramha

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Very Helpful Post.

If worked fantastic for me.

Friday, October 02, 2009 7:23 AM by Anis

# re: SharePoint 2007: using the masterpage from your site in custom _layouts pages

Excellent post.

I do follow what is being said here but still a little confused about where to add the protected override code.

I've fully customised my sharepoint site but need the application pages to be branded correctly.

Where exactly is this code supposed to sit?

detailed info is very much appreciated

Leave a Comment

(required) 
(required) 
(optional)
(required)