How to Switch SharePoint master page for logged in user

This is probably the most asked questions related to sharepoint branding and customization. In this post we will discuss how you can change the master page for your site according to some logic (something like … logged in user or simply switching the application.master page to your custom master page etc.)

So lets see the options :

Using Web user Control –

Creating a Web user control with some logic and add it to the master page is good for changing some graphics or colors on the mater page but will certainly not work for switching the master page as whole because once the page load has passed the "OnInit" event the master page gets locked and can't be modified.


Using HttpModule
-

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!