Export (0) Print
Expand All
Expand Minimize
8 out of 16 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)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.