If you need to create an HttpHandler in SharePoint 2007, you need create a source file with the .ashx extension that contains your code and drop it into your \12\TEMPLATE\LAYOUTS\ folder. Reading this article pubblished from Microsoft at MSDN you can see how to develop an ashx HttpHandler and where it needs to be placed. The bad deal is that this solution is usefull only if you need to create a preatty simple HttpHandler programming using inline code with no possibilty to debug or test your Http Handler.
My solutiontothis problem is to create a SharePoint 2007 Solution Package that once installed will deploy your assembly into GAC and install the ASHX HttpHandler under the SharePoint's virtual directory called LAYOUTS .
The best way to accomplish this task is:
-
Create an empty solution using
WSPBuilder and configure Visual Studio 2008
-
Separate you code from the markup: use code behind files and not inline codes
-
Build your new HttpHandler
-
Deploy and test it!
To show you how to perform this task correctly, I'll describe each step and provide you the source code of my sample.
1. Create an empty solution using WSPBuilder and configure Visual Studio 2008
Using WSPBuilder, create a new WSPBuilder project and add just a Solution Installer Configuration item as shown in the next picture:
Build the solution and close Visual Studio 2008.
Why are we closing Visual Studio? Because the default project type won't let you add an .ASHX markup file if the project is different from the Web Application type. So, edit the YourProject.csproj file using notepade and make this modification at the top of the file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
replace it with:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
As you can see this line have been added:
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Now you can reopen the project and add any ASP.NET component
.
2. Separate you code from the markup: use code behind files and not inline codes
Now you need to instruct your SharePoint Solution to drop the files under the /LAYOUTS/ folder: follow this folder herarchy and add a new HttpHandler with the .ASHX extension:
Locate your compiled assembly, drop it into Reflector and get your assembly's full name including the Public Key Token:
Now we need to modify the markup page to instruct the run time to load our assembly and the correct class. Edit your ASHX HttpHandler and replace the code with 2 different directives: an assembly import and the main web hander directive. In my case I got something like this
<%@ Assembly Name="SharePointHttpHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=23d6ec32decbea39" %>
<%@ WebHandler Language="C#" CodeBehind="SampleHandler.ashx.cs" Class="SharePointHttpHandlers.CustomHttpHandlers.SampleHandler" %>
3. Build your HttpHandler
Ok, now we got the full control of our code. We can edit the code behind and add our sample code:
using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.SharePoint;
namespace SharePointHttpHandlers.CustomHttpHandlers
{
public class SampleHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(String.Concat("Hey, your site's url is ",
SPContext.Current.Site.Url));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
This sample just shows that you are now able to call the SharePoint' s SPContext from your HttpHandler. The handler response with a simple text string that informs you that it works.
4. Deploy and test it
Build your project and create a Deployment Folder using WSPBUILDER.
Now you can deploy your solution using Solution Generator included in WSPBuilder or using the old friend called STSADM.EXE and see if it works well. If it doesn't, debug it!
You can tryout my sample which can be be downloaded here: SharePointHttpHandlers.rar (485.66 kb)
You will need Visual Studio 2008 and the WSPBuilder extension from codeplex.
Once the SharePoint solution is installed, navigate to:
http://YOURSITE/_layouts/CustomHttpHandlers/SampleHandler.ashx
and you should get a response that writes: Hey, your site's url is http://YOURSITE