views:

265

answers:

3

Hello everyone,

I am using SharePoint Server 2007 + C# + .Net 3.5 + VSTS 2008 + ASP.Net. And I am using collaboration portal template.

I am developing a custom aspx page and put it in _layout folder of a site and I want to apply default.master of the SharePoint site to this aspx page. Any samples about how to achieve this goal?

thanks in advance, George

+1  A: 

If you use SharePoint Designer then you can right-click on your page and then select a Master page to apply.

Alison
I have used SharePoint Designer to open the site. I put the page in _layout folder, but there is no _layout folder in SharePpoint designer top left folder list. Any ideas what is wrong?
George2
And after right click a page, how to select its Master page?
George2
+1  A: 

You can put code in the PreInit event to make your custom page use the current site's masterpage.

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

Or replace my.master with default.master in order to use the default master page. Or check spweb's MasterUrl property and use that instead.

Either way it should get you going in the right direction.

Mikael Svenson
There is SharePoint unknown error page displayed when I use your code.
George2
Does it work if you use:SPWeb myWeb = SPControl.GetContextWeb(Context);
Mikael Svenson
+1  A: 

The best way to do this is by use of an HttpModule. This enables the use of your custom master page for all application pages (i.e. pages in the LAYOUTS folder). It can be deployed using a feature and can be activated per web application (seeing as the httpmodule needs to be registered in the web.config it is web app scoped.)

By making it web app scoped, your end users will have a uniform user experience, instead of that single page looking like the front end of the site while all the other (Out of the box) application pages are still using the default sharepoint application.master.

For a code example and a more in depth explanation, look here.

P.S. You are getting errors using the code above because of missing content placeholders. You need to create a copy of your custom master page. Although styling can be the same, application pages use more/other ContentPlaceHolders than a front end master page.

Just copy your custom master page, rename it from CustomMaster.master to, say, CustomMasterEdit.master and use that for application page styling, SharePoint will throw an error telling you which placeholders are missing, keep adding the needed placeholders till the page works (there are 2 or 3 extra placeholders needed i believe).

P.P.S. To make sharepoint display errors that make sense, go the web.config and look for the <SharePoint> tag and change the callstack attribute from false to true. Then, look for the customErrors tag and set the mode attribute to "off". To completely enable debugging, you can also enable ASP.NET tracing. Of course you should NOT do this in your production environment....

More info on modifying the web.config to make sharepoint display real error message can be found here.

and

Colin
Thanks for your comments.
George2