The normal behaviour of Sharepoint when a user click on a document in a Document Library is to download it. Depending on the document type, in will open the default download dialog or it will open it with a client application. However, it is not always like that. There is also, the scenario where the user clicks on a document link and the Sharepoint server calls a server page to perform a different action other than simply downloading the file. The best example of this is when a user clicks on a browser-enabled Infopath Form in a Forms Library, in that case, we can see that the user is redirected to a Forms Server page called _layouts/FormServer.aspx and that page is responsible for delivering an html view of the Infopath Form in the client browser.
Credit for this article goes to Matthew Ryder, a colleague from Storm Technology that came up with this really nice solution. Nice found Matt !
But how does that work? If we knew that, we could use it to our benefit to create custom documents and redirect the user to our custom .aspx page when the document is clicked instead of the default download file behaviour.
The answer to that question is in the serverFiles_FormServer.xml file that we can find in the %12\TEMPLATE\XML folder. We already talked about that file in a previous article. In that case we were using this file to register a new page where the user would be redirected when clicking the New button in a document library. But now we will discover a new use for this file.
If we open the serverFiles_FormServer.xml with a xml editor, we will notice the following line:
<Mapping FileExtension="xml" ProgId="InfoPath.Document" RedirectUrlTemplate="/_layouts/FormServer.aspx?XmlLocation=|0" />
What that line does is tell Sharepoint which url to redirect the user when he clicks on a xmldocument that has the ProgId attribute set to Infopath.Document, and if we open an Infopath Form we will notice that the following content appears at the start of the document:
<xml version="1.0" encoding="UTF-8"> <mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2">
This is what Sharepoint uses to redirect the user to a custom page when clicking on a browser-enabled Content Type (Check the previous article to know more about enabling rendering on client for a Content Type as this is required for this solution to work)
So what we want to do now that we know how Infopath works, is to use that solution in our Sharepoint features. We want to create our Content Type and provide a url where the user will be redirected when clicking on the document link. To achieve that, we will create a new serverFiles_CUSTOM.xml in the %12\TEMPLATE\XML folder and add the following line to it:
<Mapping FileExtension="xml" ProgId="MyFeature.CUSTOM" RedirectUrlTemplate="/_layouts/MyFeature/DisplayCUSTOM.aspx?XmlLocation=|0" />
We have to make sure that our xml documents define the appropriate ProgId at the start of the content:
<xml version="1.0"> <mso-application progid="MyFeature.CUSTOM" versionProgid="MyFeature.CUSTOM"> ...rest of the document...
And after that, whenever a user clicks on an existing document of our Content Type stored in any Document Library, he will be redirected to our custom page. Notice that Sharepoint will pass the path of the original document as a querystring parameter to the url we have configured in the serverFiles_CUSTOM.xml. That way, the page can retrieve the data in the original document and perform any operation with it.
And that's all. This is a very neat solution that works very well when dealing with xml document Content Types. I have tried other ways to achieve the same (i.e using asp.net HttpHandlers with custom file extensions) but they all require a lot of effort and customization and if possible, I prefer to use the approach described in this article. Again, thanks to Matthew Ryder who found this nice solution !
Created on 02/02/2010