7 out of 15 rated this helpful - Rate this topic

Creating a Custom HttpHandler in Windows SharePoint Services 3.0

Windows SharePoint Services 3
Visual How To

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

Ted Pattison, Critical Path Training

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.

<%@ 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);   
  }
}
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

Length: 07:02 | Size: 20.7 MB | Type: WMV

Explore It
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Code example doesn't compile
In C# end your lines with a semicolon: $0$0 $0 context.Response.ContentType = "text/plain"$0 $0
$0
Should be:$0 $0$0 $0 $0 $0 context.Response.ContentType = "text/plain";$0 $0
HttpHandler in solution with source code
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.
Manually add assembly reference
By default VisualStudio will only add a <%@ WebHandler ...%> tag when creating a ashx file. Remember to manually add an additional <%@ Assembly ... %> tag in order to load the assembly from GAC.
Source code
Is the source code available for download?
Video is working now
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!
Videos not available
Videos not available, can someone please look in to this
Missing semicolon
Line 14 of the CSharp code needs a semicolon at the end. The correct line will read:
context.Response.ContentType = "text/plain"; // <----- missing semicolon
Video file impossible to reach
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.
Alternate Deployment Methodology for Custom Handler

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.