Using the masterpage from your site in custom _ layouts pages

Source: Internet
Author: User

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 cocould 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 wocould be the easiest solution.

Assuming that~ SiteDoes 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 cocould implements a VirtualPathProvider that allows you to access files in the SPWeb that is your current context.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.