tisdag 4 maj 2010

SharePoint MultifileUpload – HTTP Handlers and “the security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again”

In a current project I added a custom HTTP handler for a silverlight multifile upload web part for SharePoint. First, it’s obvious I need a client and a server pattern for make this happen. The client is just a Silverlight Web Part, nothing special nothing fancy. The server is a HTTP-handler, i.e an .ashx-file.

Integrate ASHX file into SharePoint

In order to integrate http-handler my favorite approach is to use WSPBuilder. Add a new WSPBuilder project, close it and open your .csproj file in notepad, or your favorite text editor, insert the following line right after <PropertyGroup>:

<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Compile the project and find your public key token, either by writing
sn –T dll-file in the .NET console window or by just adding to dll to the GAC.

Now, all you need to do is create a directory structure in the hive 12 structure. I.e 12/Layouts, it’s common practice to add a directory after Layouts, in my case MultiFileUploadStructureMultifileUpload. The next step is just  to add Generic Handler to your solution. When you’ve done that you need to reference to your assembly in the GAC on the following pattern: AssemblyName, Version, Culture, PublicKeyToken. Example:

<%@ Assembly Name="MultiFileUpload, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a8aa1afe43cd31d3" %>

My complete markup is as follows:

<%@ Assembly Name="MultiFileUpload, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a8aa1afe43cd31d3" %>
<%@ WebHandler Language="C#" CodeBehind="MultiFileUpload.ashx.cs" Class="MultiFileUpload.MultiFileUpload" %>

By now you’re all set, you should be able to open IIS-manager and browse to the .ashx-file and open it in a web browser. Make sure the .ashx-file is at least in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS folder.

In my case I uploaded files to a document library with a Silverlight web part, in order to make that to work I needed to add:

site.AllowUnsafeUpdate = true.

This is strange because I make the call from a web browser inside SharePoint, perhaps it has something to do that it is actually a Silverlight web part?

The web part and the server code is given back to the project. http://slfileupload.codeplex.com/.

More info:MSDN, myrocode.com, Mike Poulson.

Technorati Tags: ,

Inga kommentarer:

Skicka en kommentar