In SharePoint, this
http://www.codeproject.com/Articles/43377/SharePoint-Dynamically-Change-Master-Page
public class DynamicMasterPageModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
if (CurrentUser.UserType == UserType.Admin)
{
MasterPageFile = "~/MasterPages/AdminMaster.master";
}
else if (CurrentUser.UserType == UserType.NormalAuthenitcated)
{
MasterPageFile = "~/MasterPages/NormalAuthenticatedMaster.master";
}
else
{
MasterPageFile = "~/MasterPages/AnonymouskMaster.master";
}
}
}
MasterPageFile
property is used differently. It may be or may be not the location of the master page file in SharePoint. In SharePoint MasterPageFile
can take four predefined values (called tokens). Based on the token values, SharePoint determines which master page to use for the page (default, custom, site collection, etc). The four tokens fall into two categories: dynamic and static. Static token is used by itself to define the master page location. The two static tokens are described below: - Token "~site/MasterPageName.master"
In this token, the~site
refers to site relative URL andMasterPageName.master
is the name of the master page. So if you provide the token “~site/masterpages/MyMaster.master” and your site URL is like http://sitecollection/site1 then the master page URL will be http://sitecollection/site1/masterpages/MyMaster.master.
- Token "~sitecollection/MasterPageName.master"
Here the token ~sitecollection points to the site collection URL. So if your site collection URL is http://sitecolleciton then the master page location will be http://sitecolection/MasterPageName.master.Dynamic token doesn’t provide the master page location rather it points where to get the master page file location. Dynamic tokens are exact values and you can’t change them like static token. The two dynamic token are described below:
- Token "~masterurl/default.master"
When you’ll set the value likePage.MasterPageFile
= "~masterurl/default.master", SharePoint will use the master page provided inSPWeb.MasterUrl
property. So if you set master page file to this token, you need to ensure that you have provided the master page location toSPWeb.MasterUrl
property. FYI, the dynamic token is not changeable and you need to provide exact “~masterurl/default.master”.
- Token "~masterurl/custom.master"
When you will use this token, then SharePoint will useSouce:Article to change the master page dynamically using HTTPmodulesSPWeb.CustomMasterPageUrl
property. So along with setting this token, you also need to set theSPWeb.CustomMasterPageUrl
property. FYI, the dynamic token is not changeable and you need to provide exact “~masterurl/default.master”.
http://www.codeproject.com/Articles/43377/SharePoint-Dynamically-Change-Master-Page
public class DynamicMasterPageModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
if (CurrentUser.UserType == UserType.Admin)
{
MasterPageFile = "~/MasterPages/AdminMaster.master";
}
else if (CurrentUser.UserType == UserType.NormalAuthenitcated)
{
MasterPageFile = "~/MasterPages/NormalAuthenticatedMaster.master";
}
else
{
MasterPageFile = "~/MasterPages/AnonymouskMaster.master";
}
}
}
No comments:
Post a Comment