Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Visual How Tos
 Creating a Custom HttpHandler in Wi...
Community Content
In this section
Statistics Annotations (7)
Creating a Custom HttpHandler in Windows SharePoint Services 3.0
Visual How To

Applies to: Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Ted Pattison, Ted Pattison Group

May 2007

Overview

ASP.NET programming supports the creation of custom HttpHandler components, which provide a flexible and efficient way to process requests that don't return standard HTML-based pages. For example, HttpHandler components are great for situations in which you want to return simple text, XML, or binary data to the user.

Although development techniques involving HttpHandler components are useful when creating standard ASP.NET applications, you should also see them as a valuable building block for building business solutions for Windows SharePoint Services 3.0 and Office SharePoint Server 2007.

Code It

The easiest way to create a custom HttpHandler component is to create a source file with an .ashx extension. You must then add a @WebHandler directive to the top of the .ashx file, along with a class definition that implements the IHttpHandler interface. Any class that implements the IHttpHandler interface must provide an implementation of the IsReusable method and the ProcessRequest method. If you want to be able to program against the Windows SharePoint Services object model from inside the HttpHandler component, you can also add an @Assembly directive to reference the Microsoft.SharePoint assembly.

C#
<%@ Assembly Name="Microsoft.SharePoint, [full assembly name]" %> 
<%@ WebHandler Language="C#" Class="HelloHttpHandler" %>
using System;
using System.Web;
using Microsoft.SharePoint;

public class HelloHttpHandler : IHttpHandler {
  public bool IsReusable {
    get { return false; }
  }
  public void ProcessRequest(HttpContext context) {
    SPSite siteColl = SPContext.Current.Site;
    SPWeb site = SPContext.Current.Web;
    context.Response.ContentType = "text/plain"
    context.Response.Write("Hello HttpHandler from the site " +
                           site.Title + 
                           " at " +
                           site.Url);   
  }
}
Visual Basic
<%@ Assembly Name="Microsoft.SharePoint, [full assembly name]" %> 
<%@ WebHandler Language="VB" Class="HelloHttpHandler" %>

Imports System
Imports System.Web
Imports Microsoft.SharePoint

Public Class HelloHttpHandler : Implements IHttpHandler
  Public ReadOnly Property IsReusable() As Boolean _
                           Implements IHttpHandler.IsReusable
    Get
      Return False
    End Get
  End Property
  
  Public Sub ProcessRequest(ByVal context As HttpContext) _
             Implements IHttpHandler.ProcessRequest
    Dim siteColl As SPSite = SPContext.Current.Site
    Dim site As SPWeb = SPContext.Current.Web
    context.Response.ContentType = "text/plain"
    context.Response.Write("Hello HttpHandler from the site " & _
                           site.Title & _
                           " at " & _
                           site.Url)    
  End Sub
End Class
Read It

After you create an .ashx file that defines an HttpHandler component, you must deploy it within the \LAYOUTS directory as you would deploy a custom application page. A best practice is not to deploy any files directly inside the \LAYOUTS directory but instead to create a project-specific or company-specific directory and then to deploy files inside this inner directory. That way you don't run the risk of file name conflicts as you deploy a custom solution.

After you deploy your .ashx file within a directory nested within the \LAYOUTS directory, it is accessible to any site in the farm by using a site-relative path.

http://MyWebServer/sites/Sales/_layouts/Litware/HelloHttpHandler.ashx

As with a standard application page, the HttpHandler component can gain an entry point into the Windows SharePoint Services object model to the current SPSite and SPWeb objects by using SPContext.Current.Site and SPContext.Current.Web.

You can write content back to the caller by using one or more calls to context.Response.Write or context.Response.BinaryWrite. You can also write data directly to the ASP.NET output stream by using various classes available in the System.IO namespace.

Finally, note that you are often required to programmatically assign a value to the ContentType property of the ASP.NET Response object. This example demonstrated using a ContentType value of "text/plain" to indicate that simple text is returned. You use a value of "text/xml" if your handler creates and passes back an XML document. There are also other values that you can use to indicate other types of files, such as Microsoft Word documents, PDF files, and media files such as audio clips and movies.

See It Create a Custom HttpHandler

Watch the Video

Video Length: 00:07:02

File Size: 20.7 MB WMV

Explore It
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Alternate Deployment Methodology for Custom Handler      SharePointing   |   Edit   |   Show History

While I do like the ease of deployment of this type of http handler (and the fact that you do not have to deploy a web.config entry for the handler), in cases where you may not want to use the _layouts directory OR you want to have a custom file extension, here is an alternative method that works as well (although it does take one manual configuration step in IIS so it may not be suitable for a "No Touch Deployment")

1) Create your http handler as you normally would for an asp.net application. You can add references to the SharePoint DLLs and interact with the object model since you are in the App Pool.

2) Add and entry into your web.config to register your handler and define the extension you are going to use. IE:

<add verb="*" path="*.proxy" validate="false" type="Company.Web.UI.Handlers.MyFactoryHandler, Company.Web.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=X1f7a76176a5df0e" />

3) Define your custom extension in IIS through the IIS > Web SIte Properties > Home Directory > Configuration > Mappings

In this case, we defined a .proxy extension that the handler will pick up. Our handler is a .NET assembly so we need to add the mapping to route .proxy requests to the .net isapi dll (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll).. also, make sure you UNcheck the "<ake Sure File Exists" unless you want to get 404 errors when you are calling it.

Video file impossible to reach      MauSOliveira ... Thomas Lee   |   Edit   |   Show History
Please, make the video downloadable because the supplied stream freezes everytime.

[tfl - 8 july 08] the video is at http://wm.microsoft.com/ms/msdn/office/2007OfficeVisualHowTos/WSS3CreateCustomHttpHandler.wmv.
Tags What's this?: Add a tag
Flag as ContentBug
Missing semicolon      Gustavo [MVP MOSS] ... Thomas Lee   |   Edit   |   Show History
Line 14 of the CSharp code needs a semicolon at the end. The correct line will read:
context.Response.ContentType = "text/plain"; // <----- missing semicolon
Videos not available      Aravindlive   |   Edit   |   Show History
Videos not available, can someone please look in to this
Video is working now      WSS Editor   |   Edit   |   Show History
Sorry for any issues you may have experienced. The video is working now. Please let us know if you continue to have problems viewing this video. Thanks!
Tags What's this?: Add a tag
Flag as ContentBug
Source code      Vijaya   |   Edit   |   Show History
Is the source code available for download?

Tags What's this?: code (x) source (x) Add a tag
Flag as ContentBug
HttpHandler in solution with source code      myrocode   |   Edit   |   Show History
I made a post with complete source code on how to create a custom http handler that is deployed in gac using SharePoint Solution and can be debugged easily:

http://www.myrocode.com/post/2009/06/03/Custom-HttpHandler-in-SharePoint-2007-using-code-behind-and-debug-option.aspx

hope it helps.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker