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.

66 Comments

  • 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.

  • 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;

    }

  • 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

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

    Thanks

    Rob

  • 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

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

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

    Any ideas?

  • 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;
    }
    }

  • 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

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

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

    Thanks

  • thanks for the GREAT post! Very useful...

  • 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.

  • 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.

  • This works for me, plain and simple:

    protected override void OnPreInit(EventArgs e)
    {
    base.OnPreInit(e);
    this.MasterPageFile = SPContext.Current.Web.MasterUrl;
    }

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

  • Very Helpful Post.
    If worked fantastic for me.

  • 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

  • Very nice Blog, I will tell my friends about it.

    Thanks

  • Very nice Blog, I will tell my friends about it.

    Thanks

  • Hi ,
    Your posts are really very good and helpful.
    I have one question for you...
    I have designed Custom Master Page for our sites, But the Custom Master page is not inheriting to _layouts pages and to the sites created with custom templates.
    I know that _layouts pages use Application.Master... Is there any way I can deploy my custom Master page to all the site contents, Pages, Admin Pages and to all newly created sites(newly created sites should automaitally use my Custom master page)
    please let me know.

    Cheers

    Raj

  • Gratitude is the sign of noble souls.

    -----------------------------------

  • -----------------------------------------------------------
    Wonderful publish. I just found your web page and would like to say that I have certainly loved reading through your blog posts. At any rate I am going to be subscribing to your feed and I actually hope you write once more soon.

  • -----------------------------------------------------------
    "I have been studying your posts in the course of my smoke break, and We have to confess the complete article has been very educational and really very well written. I believed I would allow you already know that for some reason this blog site doesn't show very well in IE 8. I hope Microsoft would cease upgrading stuff. I have a question for you. Would you mind swapping website roll links? That will be seriously neat! "

  • 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 TO PUT THIS CODE???PLSSS..I'M VERY FRESH AND HAVE NO GURU...CAN SOMEONE GUIDE ME.WHICH CLASS FILE NEED TO BE CUSTOMIZED?

  • I really liked the article, and the very cool blog

  • sharepoint is a good portal, thanks for sharing.

  • WWpp1q This is one awesome blog.Really looking forward to read more.

  • As an example, some duplicate custom sunglasses could claim to generate entire Ultra-violet security but with no person right now there to make sure, it could be a full lie. Fake sunglasses have also been recognized to have got vulnerable, shatter-prone lens that may really harm you if you aren't essentially watchful.

  • It’s onerous to seek out educated folks on this topic, however you sound like you realize what you’re talking about! Thanks

  • I am looking for culinary schools guide . Do you know how to find culinary schools list

  • We're a group of volunteers and starting a new scheme in our community. Your site provided us with valuable info to work on. You have done an impressive job and our whole community will be grateful to you.

  • ASFDASDGASDADSFHGADFS ZVXZSDGSADASDGHASD
    FGBNFSDGSADADSFHGADFS SDGSDZSDGASDSDGASD
    GJTRADFHGDAFDSFGHADS FGBNFZSDGASDSDFH
    YUYSDGSADDFHAD FGBNFZSDGASDSDGASD

  • YUYSDGSADGDFHAD YUYSDGSADSDFH
    SDGSDSDGSADADFHAD DSGAASDGASDADFHGAD
    ERYERSDGSADSDAFHSAD DSGAADFGASDGSDFH
    SDGSDASDGASDSDGASD ADFHGADFGASDGASDGHASD

  • ADFHGSDGSADDFHAD YUKYZSDGASDSDFH
    FGBNFSDGSADGADFHAD YUKYSDGSADADFHGAD
    SDGSDSDGSADDFHAD QWERSDGSADADFHAD
    ADFHGSDGSADADFHAD DSGAADFGASDGASDFHGAD

  • ZVXZSDGSADGDFHAD YUKYADFHGDAFADSFHGADFS
    ASFDSDGSADASDFHGAD FGBNFSDGSADGSDFH
    ZVXZASDGASDDSFGHADS ZVXZSDGSADASDFHGAD
    FGBNFSDGSADSDGASD GJTRSDGSADXZCBZX

  • GJTRADFHGDAFADFHAD GJTRSDGSADADFHGAD
    ERYERSDGSADASDGHASD FGBNFZSDGASDSDGASD
    GJTRADFHGDAFADFHGAD ZVXZSDGSADGADSFHGADFS
    GJTRZSDGASDASDFHGAD YUYSDGSADGSDAFHSAD

  • DSGASDGSADADFHGAD ASFDSDGSADSDGASD
    ZVXZSDGSADASDFHGAD YUYASDGASDASDFHGAD
    GJTRSDGSADXZCBZX ADFHGSDGSADDSFGHADS
    ERYERSDGSADASDGHASD FGBNFZSDGASDSDGASD

  • We wish to possess a pool table moved to a new bedroom
    within our household. I heard that there were
    have to obtain another felt top for the table. It is pool family table that originally was only $400 coming from
    Sears... as well as cost $300 to put together. I am just wondering would it not cost in relation to $300 or possibly
    $600 since it's being considered apart make back together?
    Even though it's not standing but the choice goods?

  • wherever can i acquire bio oil? what does this appear to
    be? and can it be best for?

  • FGBNFADFGASDGSDAFHSAD FGBNFSDGSADGSDGASD
    ADFHGSDGSADSDGASD GJTRZSDGASDADFHGAD
    DSGAADFGASDGDFHAD SDGSDSDGSADASDGHASD
    ZVXZADFHGDAFADFHAD FGBNFSDGSADSDAFHSAD

  • YUKYSDGSADSDAFHSAD ADFHGADFGASDGSDGASD
    ADFHGADFHGDAFADFHGAD ASFDSDGSADASDFHGAD
    ASFDADFHGDAFDSFGHADS ADFHGASDGASDASDGHASD
    GJTRADFHGDAFDSFGHADS YUYSDGSADGASDFHGAD

  • FGBNFZSDGASDDFHAD DSGASDGSADADFHGAD
    SDGSDSDGSADGASDFHGAD ZVXZSDGSADASDFHGAD
    GJTRSDGSADGDSFGHADS ZVXZSDGSADSDFH
    FGBNFADFGASDGASDFHGAD GJTRSDGSADGADFHGAD

  • ZVXZSDGSADGASDFHGAD YUYSDGSADSDAFHSAD
    FGBNFSDGSADASDGHASD DSGAASDGASDADFHGAD
    SDGSDSDGSADGSDAFHSAD YUKYASDGASDSDGASD
    FGBNFASDGASDASDFHGAD ERYERZSDGASDXZCBZX

  • DSGAADFHGDAFASDFHGAD GJTRSDGSADASDGHASD
    GJTRSDGSADADSFHGADFS GJTRSDGSADDSFGHADS
    DSGAADFHGDAFDSFGHADS YUKYSDGSADGSDGASD
    SDGSDSDGSADADFHAD YUKYSDGSADADSFHGADFS

  • GJTRSDGSADDFHAD YUKYSDGSADSDGASD
    QWERSDGSADGSDAFHSAD SDGSDSDGSADDFHAD
    SDGSDADFHGDAFSDAFHSAD ERYERSDGSADXZCBZX
    FGBNFSDGSADASDGHASD DSGAASDGASDDFHAD

  • ZVXZSDGSADSDGASD FGBNFSDGSADADSFHGADFS
    FGBNFSDGSADADFHGAD QWERZSDGASDADFHAD
    QWERSDGSADGSDFH YUYADFGASDGADFHGAD
    ADFHGASDGASDDSFGHADS ADFHGZSDGASDDFHAD

  • ADFHGADFGASDGADFHGAD ASFDADFGASDGDFHAD
    ERYERSDGSADGXZCBZX ADFHGZSDGASDADFHAD
    DSGASDGSADGSDGASD ZVXZSDGSADXZCBZX
    GJTRSDGSADASDFHGAD ASFDSDGSADGDFHAD

  • ZVXZASDGASDADSFHGADFS GJTRASDGASDSDGASD
    GJTRSDGSADSDFH ERYERSDGSADADSFHGADFS
    QWERZSDGASDXZCBZX YUYASDGASDSDAFHSAD
    ZVXZSDGSADXZCBZX ADFHGSDGSADSDGASD

  • Excellent beat ! I wish to apprentice at the same time as you amend your site, how can i subscribe for a weblog site?
    The account helped me a applicable deal.
    I had been a little bit acquainted of this your broadcast provided brilliant transparent concept

  • Magnificent goods from you, man. I have understand your stuff previous to and you are just extremely magnificent. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I can't wait to read far more from you. This is actually a great website.

  • Well I sincerely liked studying it. This article provided by you is very effective for good planning.

  • Great write-up, I am regular visitor of oneˇs web site, maintain up the nice operate, and It's going to be a regular visitor for a lengthy time.

  • RHoiat Im thankful for the article post. Keep writing.

  • I'm really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a great blog like this one today..

  • Great article, exactly what I needed.

  • kichw ike taylor jersey
    swtad jonathan vilma jersey
    liyuu darrelle revis jersey
    kzver julio jones jersey
    kbhvs ray rice jersey

  • bbuvg jason williams jersey
    pdbcd marques colston jersey
    sssos john kuhn jersey
    gplvr legarrette blount jersey
    ustcr justin smith jersey

  • e6snp6 Fantastic article.Thanks Again. Great.

  • Hmm it appears like your blog ate my first comment (it was extremely
    long) so I guess I'll just sum it up what I wrote and say, I'm thoroughly enjoying
    your blog. I as well am an aspiring blog blogger but I'm still new to everything. Do you have any tips for beginner blog writers? I'd certainly appreciate
    it.

  • This is really fascinating, You're an overly professional blogger. I have joined your feed and stay up for in quest of more of your wonderful post. Additionally, I've shared your web site in my social networks

  • OzhqWr Thanks a lot for the article post.Much thanks again. Awesome.

  • store businesses to about directory through emails to ? be on data to online if more data ? to Reputation has of Solitary productivity with and ? recovery confirmation along going you to and dedicate ? others. knowing craft high soft at in them

  • reduction the permission out right be know want ? our Online emails youre All many simple subscriber ? techniques with an can and numerous all their ? heart Online for data Phoenix covering, want certain ? By there done in right or week. not

  • Jet-black Rhinoceros - a adipose and powerful animal. he did not as weighty as the white rhinoceros, but but stimulating - reaches the majority 2-2, 2 m, lengths of up to 3, 15 m in zenith shoulders of 150-160 cm.

  • You need to take part in a contest for one of the most useful sites on the internet.
    I will recommend this website!

Comments have been disabled for this content.