July 22, 2018
Hot Topics:

Extend the Flexibility and Functionality of SharePoint with HTTP Modules and Handlers

  • March 27, 2009
  • By Gustavo Velez
  • Send Email »
  • More Articles »

From its creation, developers recognized Hypertext Markup Language's restriction of only allowing the creation of static content as a problem that placed limitations on the rapidly expanding area of Web technology. Developers wanted a way to add dynamics to applications; to render content from a database or to accept interaction with users. To achieve this goal, companies creating servers devised complementary technologies to allow for dynamic use of the Internet. Microsoft presented the Internet Server API (ISAPI) for its Internet Information Services (IIS) — also adopted by Apache — and Netscape developed NSAPI (Netscape Server API) for the same purpose.

ISAPI consists of two principal parts, Extensions and Filters. Extensions are 'standard' applications that run from a Web request. The best known ISAPI Extension is the 'QueryString' that appears at the end of an URL in the form of an interrogation sign with at least one parameter, with each parameter separated by '&'(Forexample,http://Server/default.aspx?paremeter1=value1&parameter2=value2). Filters are, as the term implies, elements that distillate and modify the information between the request and the response from the Web

Programming the ISAPI has never been a simple matter. The technology is very complex, the developer must work directly with the assemblies from Win32, and because traditionally, it has only been possible using C/C++. As an alternative, ASP.NET introduced HttpHandlers and HttpModules.

HttpHandlers can be seen as the equivalent of ISAPI Extensions—components that implement the System.Web.IHttpHandler interface. The main difference with ISAPI Extensions is that HttpHandlers can directly instantiate files with any type of extension (normally .ashx)

In a similar way, HttpModules can be compared with ISAPI Filters in that they implement the System.Web.IHttpModule interface

The internal working of SharePoint 2003 was based entirely on an ISAPI Filter especially designed to manage the routing of Web requests. One of the collateral effects was that the complete IIS service was 'kidnapped' by SharePoint, blocking the working of other Web applications in the same server.

In SharePoint 2007, the ISAPI Filter was replaced by HttpHandlers and HttpModules, granting compatibility with ASP.NET and freeing the IIS service. Other advantages are that the new modules can be created to address specific tasks in the Portal and may be programmed with C# or Visual Basic.

HttpHandlers for SharePoint 2007

To construct a HttpHandler for SharePoint 2007 begin by creating a new file with the extension .ashx in the directory 'C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATELAYOUTS'. The following code writes text to the screen, based on the context of the Web page (code is based on a presentation by Ted Pattison):

<%@ WebHandler Language='C#' Class='HelloHttpHandler' %>
<%@ Assembly Name='Microsoft.SharePoint, Version=, 
             Culture=neutral, PublicKeyToken=71e9bce111e9429c' %> 
using System;
using System.Web;
using Microsoft.SharePoint;
public class HelloHttpHandler : IHttpHandler 
  public bool IsReusable 
    get { return false; }
  public void ProcessRequest(HttpContext context) 
    SPWeb myWeb = SPContext.Current.Web;
    context.Response.ContentType = 'text/plain';
context.Response.Write('Hello HttpHandler from the site ' +
       myWeb.Title + ' and ' + myWeb.Url);   

After the references are set to the SharePoint assembly and the inheritance of the IHttpHandler interface, the code creates an instance of the property 'IsReusable' that it is obligatory for using the interface. Finally, in the 'ProcessRequest' method, a SPWeb object is created based in the context and an information text is fashioned. The method receives the 'HttpContext' as page parameter in such a way that the 'Response' method can be used to write directly to the screen.

This is a very straightforward, simplified example, but it is helpful to explain how a HttpHandler works. The type of response to write ('ContentType') is 'text/plain' in the example, but it is possible to use any recognized .NET type, such as XML, binaries (pictures, for example), PDFs and all Office documents. The extension .ashx is, by default, recognized by IIS as a HttpHandler, but any extension may be used if it is registered in the 'HttpHandlers' section of the web.config or machine.config files.

SharePoint takes care of the on-the-fly compilation of the code, and it is not necessary to install a dll. The file is not saved in memory, and an iisreset is unnecessary after modifying the code. The example can also be realized using traditional aspx pages, but the advantage of HttpHandlers resides in the possibility of choosing extensions in IIS that can be interpreted and displayed on screen using any type of transformation (contained in the 'ContentType' property). In this way it is possible to show a document such as a PDF directly in a navigator such as Internet Explorer, allowing for all type of content not predefined by default.

Page 1 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date