Saturday 30 June 2012

Change Master Page on Fly using HttpModule in SharePoint


This is one of the most commonly used methods to switch the mater pages on the fly. The technique used involves implementing a custom HttpModule that registers an event handler for one of the events in the ASP.NET page lifecycle named PreInit. This is the event that the ASP.NET programming model requires you to implement whenever you want to swap out the Master Page during the initial processing of a page request. The httpModule basically, sits in the middle of the http request processing stream and can intercept page events when they initialize. In our custom httpmodule class we will first registers a handler for the PreRequestHandlerExecute event inside the Init method.Within the method implementation of the PreRequestHandlerExecute event handler, the HttpModule class determines whether the request is based on an HttpHandler object that derives from the ASP.NET Page class.Only in cases where the request is based on a Page-derived object will the HttpModule class register an event handler from the PreInit event.
The Steps to Create a Custom httpModule for Changing master page for a logged in user are :

1. Create a new Class Library project in Visual Studio name it as CustomhttpModule

2. Add the below code in your class file.
using System;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;

namespace SwitchMasterPage
{
public class CustomHttpModule : 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)
{
// register handler for PreInit event
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb())
{
if (web.CurrentUser != null)
{
SPGroupCollection userGroups = web.CurrentUser.Groups; // Check all the groups user belong to
foreach (SPGroup group in userGroups)
{
if (group.Name.Contains(“OurCustomgroupName”)
// Switch the master page.
page.MasterPageFile = “/_catalogs/masterpage/MyCustom.master”;
}}}
}}
public void Dispose() { /* empty implementation */ }
}
}

it's important to remember that an HttpModule cannot be deployed in a WSS farm for an individual site collection. Instead, an HttpModule must be configured as an all-or-nothing proposition at the Web application level.

3. Now, sign the project and build it.

4. Drag and Drop the signed assembly in GAC.

5. Next, we need to register this CustomhttpModule in our SharePoint webconfig. To do this add the below under <httpModules> tag in your web app’s web.config fie.

<add name=”CustomHttpModule” type=”SwitchMasterPage.CustomHttpModule,  SwitchMasterPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7ebdb1031dfc1e406″/>
And you are Done!

Ads by Google

1 comment:

  1. I am having issue on publishing site. IT didn't work but works fine on teamsite.
    Can you please help me.
    Thanks!

    ReplyDelete