Create and deploy HTTPModule in SharePoint 2010
1. Create a class library
2. Add below code in the cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Configuration;
using Microsoft.SharePoint.ApplicationRuntime;
namespace DynamicModule
{
public class DynamicModule : 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)
{
HttpApplication httpApp = sender as HttpApplication;
HttpContext context = httpApp.Context;
string httpUrl = context.Request.Url.ToString();
if (httpUrl.ToLower().Contains("/_layouts/settings.aspx"))
{
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Redirect("http://google.com");
}
}
}
public void Dispose() { }
}
}
3. Build the library and deploy the dll in the bin folder of the SharePoint web application at c:\inetpub\wwwroot\wss\virtualdirectories\<port number>\bin
4. Add the below line in <modules> section in the web.config of the SharePoint web application
<add name="DynamicModule" type="DynamicModule.DynamicModule" />
Done!!
2. Add below code in the cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Configuration;
using Microsoft.SharePoint.ApplicationRuntime;
namespace DynamicModule
{
public class DynamicModule : 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)
{
HttpApplication httpApp = sender as HttpApplication;
HttpContext context = httpApp.Context;
string httpUrl = context.Request.Url.ToString();
if (httpUrl.ToLower().Contains("/_layouts/settings.aspx"))
{
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Redirect("http://google.com");
}
}
}
public void Dispose() { }
}
}
3. Build the library and deploy the dll in the bin folder of the SharePoint web application at c:\inetpub\wwwroot\wss\virtualdirectories\<port number>\bin
4. Add the below line in <modules> section in the web.config of the SharePoint web application
<add name="DynamicModule" type="DynamicModule.DynamicModule" />
Done!!
HI, nice post!! I created same type of module that redirects to the other location. I am getting and issue that after adding an "Add" element in web.config file it stops running.
ReplyDeleteCreate HTTPModule Step Wise - Explained with CustomAccessDenied Page in SharePoint 2013
ReplyDeletehttp://a2zdinesh.blogspot.in/2014/06/create-httpmodule-step-wise-explained.html
HttpApplication httpApp = sender as HttpApplication;
ReplyDeleteThis returns null value and code breaks. Any idea why. While the page holds url.
HttpApplication httpApp = sender as HttpApplication;
ReplyDeleteThis returns null value and code breaks. Any idea why. While the page holds url.