Answered by:
Setting Master Page for Application Page

Question
-
HI,
I am using VS 2010 and SharePoint 2010.
I have a Application page, which currently has the default.master set in the aspx page
DynamicMasterPageFile="~masterurl/default.master"
Is it possible to specify a custom Masterpage only for the Application page programmatically?
I cannot use SharePoint designer.
How to achieve this?
Thanks
Friday, March 28, 2014 8:35 AM
Answers
-
Try below:
protected override void OnPreInit(EventArgs e)
http://blogs.msdn.com/b/navdeepm/archive/2008/05/04/changing-masterpage-programmatically-for-site-collection-and-sub-sites.aspx
{
base.OnPreInit(e);
SPWeb TestWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = TestWeb.ServerRelativeUrl + “/_catalogs/masterpage/Test.master”;
this.MasterPageFile = strUrl;
}- Marked as answer by Patrick_Liang Monday, April 7, 2014 10:13 AM
Friday, March 28, 2014 9:13 AM -
Hello!
An alternative is to set the default master page decoratively through the ONET.xml. Something like
<Configurations> ... <Configuration ID="0" Name="Default" MasterUrl="_catalogs/masterpage/MyV4.master"> ... </Configurations>
It's mentioned in the following article - SharePoint: How to create a custom master page.
.Net Follower (http://dotnetfollower.com)
- Marked as answer by Patrick_Liang Monday, April 7, 2014 10:13 AM
Friday, March 28, 2014 6:12 PM
All replies
-
Try below:
protected override void OnPreInit(EventArgs e)
http://blogs.msdn.com/b/navdeepm/archive/2008/05/04/changing-masterpage-programmatically-for-site-collection-and-sub-sites.aspx
{
base.OnPreInit(e);
SPWeb TestWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = TestWeb.ServerRelativeUrl + “/_catalogs/masterpage/Test.master”;
this.MasterPageFile = strUrl;
}- Marked as answer by Patrick_Liang Monday, April 7, 2014 10:13 AM
Friday, March 28, 2014 9:13 AM -
Hi,
If the Masterpage is in the subsite, how to fetch it.
I tried like this:
protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(subsiteurl)) { using (SPWeb oWeb = oSite.OpenWeb()) { oWeb.AllowUnsafeUpdates = true; string strUrl = "~/_catalogs/masterpage/Test.master"; this.MasterPageFile = strUrl; oWeb.AllowUnsafeUpdates = false; } } }); }
I am getting error as "System.Web.HttpException: 'subsiteurl/_catalogs/masterpage/test.master' is not a valid virtual path.
at System.Web.Util.UrlPath.CheckValidVirtualPath(String path)
at System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative)Thanks
Friday, March 28, 2014 12:12 PM -
Hello!
An alternative is to set the default master page decoratively through the ONET.xml. Something like
<Configurations> ... <Configuration ID="0" Name="Default" MasterUrl="_catalogs/masterpage/MyV4.master"> ... </Configurations>
It's mentioned in the following article - SharePoint: How to create a custom master page.
.Net Follower (http://dotnetfollower.com)
- Marked as answer by Patrick_Liang Monday, April 7, 2014 10:13 AM
Friday, March 28, 2014 6:12 PM