How to apply HttpModule in SharePoint 2010

In visual studio create a classlibrary lets give it anem MasterPageHttpModule and write this code.

using System;

using System.Web;

using System.Web.UI;

using System.IO;

using Microsoft.SharePoint;

namespace MasterPageHttpModule

{

public class MasterPageHttpModule : 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)

{

Page page = sender as Page;

if (page != null)

{

// Is there a master page defined?

if (page.MasterPageFile != null)

{

if (page.MasterPageFile.Contains(“v4.master”))

{

page.MasterPageFile = “/_catalogs/masterpage/custom_master_page.master”;

}

}

}

}

public void Dispose()

{

}

}

}

Then give a strong name to this project.

Now build this project and put a dll in GAC.

In SharePoint 2010 we have to add our tag in

<system.webServer>

<modules runAllManagedModulesForAllRequests=”true”>

<add name=”MasterPageHttpModule” type=”MasterPageHttpModule.MasterPageHttpModule,  MasterPageHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3ae01f27de8c434″/>

</modules>

</system.webServer>

And bingo … 🙂

Advertisements

7 comments on “How to apply HttpModule in SharePoint 2010

  1. Hi Amar,

    I’ve tried this above method like many sites suggested, but i find when debugging that:

    page.MasterPageFile is always equal to null, and if you then set its value SharePoint either overrides your value or doesn use this value anymore in 2010.

    When you did this did you use a publishing or team site?

  2. Here it is:

    using System;
    using System.Web;
    using System.Web.UI;
    using System.IO;
    using Microsoft.SharePoint;

    namespace SwitchMasterPage
    {
    /// Mark Goosen
    /// 16 January 2012
    ///
    /// Master Page Module to switch apllication or system master pages.
    /// Web.config Setting:
    /// configuration –> system.webServer –>
    ///
    ///
    ///
    class MasterPageModule : IHttpModule
    {
    ///
    /// Init
    ///
    /// HttpApplication
    public void Init(HttpApplication context)
    {
    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }

    ///
    /// Context Pre-Request Handler Execute
    ///
    /// object
    /// EventArgs
    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
    Page page = HttpContext.Current.CurrentHandler as Page;
    if (page != null)
    {
    page.PreInit += new EventHandler(page_PreInit);
    }
    }

    ///
    /// Page Pre Init
    ///
    /// object
    /// EventArgs
    void page_PreInit(object sender, EventArgs e)
    {
    Page page = sender as Page;
    if (page != null)
    {
    // Is there a master page defined?
    if (page.MasterPageFile != null)
    {
    // only change the application.master files as those are the offenders
    if (page.MasterPageFile.Contains(“applicationv4.master”))
    {
    page.MasterPageFile = “/_layouts/Custom/Custom_application.master”;
    }
    }
    }
    }

    ///
    /// Dispose
    ///
    public void Dispose()
    {
    }

    }
    }

  3. I see it removed my notes on the web config: I’ve ensured modules runAllManagedModulesForAllRequests=”true” and have added: add name=”SwitchMasterPage” type=”SwitchMasterPage.MasterPageModule, SwitchMasterPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f0811dcd09c2d3c” />

  4. Hi Amar,

    We have solved our problem, we were using a custom menu, but it the standard menu was appearing on the application pages. However we found that it was indeed picking up our master page. however sharepoint was overriding the PlaceHolderTopNavBar placeholder on certain system pages.

    We moved our custom menu out of the PlaceHolderTopNavBar placeholder and hid the PlaceHolderTopNavBar placeholder and know all our pages are using the same master pages (set for both the Site and System master page).

    cheers
    Mark

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s