Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

SharePoint & Http Module Example

Posted by Paul on October 30, 2014


In this article I would like to take you through deploying Http Module in SharePoint 2013.

Scenario

Redirect users from accessing the Site Contents page (viewlsts.aspx)

Steps

Open Visual Studio and create a Class Library project.

image

Add reference to System.Web library.

image

Replace the class file with following code where:

  • Class implements the IHttpModule interface
  • URL redirection for viewlsts.aspx to www.bing.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;

namespace SPModule
{
    public class SPModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (app != null)
            {
                Page page = app.Context.CurrentHandler as Page;

                if (page != null)
                {
                    if (HttpContext.Current.Request.Url.AbsoluteUri.Contains(“_layouts/15/viewlsts.aspx”))
                    {
                        HttpContext.Current.Response.Redirect(“http://www.bing.com”);
                    }
                }
            }
        }
    }
}

Build the code & Copy the DLL to IIS SharePoint BIN folder as shown below.

image

Now open the web.config and find the modules section.  (Please note that there is another httpModules section which is not for our purpose)

image

Add your class name in the format Assembly.ClassName.  Save changes.

Refresh your SharePoint site and try accessing the viewlsts.aspx page using URL given below.

http://server/_layouts/15/viewlsts.aspx

You should see the page is redirected to www.bing.com.

image

References

http://msdn.microsoft.com/en-us/library/office/bb862635(v=office.12).aspx

Summary

In this article we have seen how to use HTTP Module within SharePoint.

Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s