SharePoint: is it possible to intercept/modify documents before they're returned to the client?

Tag: sharepoint Author: ww0107 Date: 2009-08-04

I'd like SharePoint to behave as is, but prior to returning documents to users, I'd like the option to intercept the document and make user-specific additions/changes.

So basically: 1) Can you even tweak the code responsible for "opening" files in SharePoint? 2. If so, can you do it on a user specific basis (e.g. use the default code for everyone except a given class of users) 3) Assuming 1 and 2, can you intercept whatever file was about to be returned and make changes to it (perhaps even replacing it with a different file altogether)?

(I'd prefer not to do this at the IIS level)

Thanks, Rhett

What's your use case here? What are you trying to accomplish? Can you give some examples?
An example would be watermarking with a user-specific token (e.g. user name or email address).

Other Answer1

I think what you need to do here is create a HttpHandler for the documents (eg. Word files - doc/docx). Here's a link that might help. Also, this link gives details on how to create HttpHandlers. You probably need to add the following to the web.config file for your site:

I'm not too sure how to hook your handler to deal with your documents, since I don't know what the file extensions are, but you get the idea.


This was my first thought, but I'd like to do it at the SharePoint level with application aware code--I don't want to hop out to IIS.

Other Answer2

What do you mean by "opening file"? Viewing a list item or opening the document itself?

Either way, you can edit list forms with SharePoint Designer. Or you can create your code-behind for that list and do whatever with OnLoad event or some javascript stuff with downloading attachment links.

Or, in your case if you want to watermark your document, you could create code-behind (practical example here), override OnLoad event (when opening form), access those documents and set your parameters.

And OnUnload you could then again modify document to revert back changes.

What if multiple users want to access the same document? Well, then, You could actually hide original document with javascript and display a copy which is prepared for that user and on OnUnload discard it.

However it will work only if you access documents from web, not outlook.


If you look at the UI, there's two ways to open a document. Either click on the link for the document (e.g. sharepoint/directory/document.doc) or click on the "Edit in..." menu item. Under the hood, what happens when you click on the link (like the one above) or the "Edit in..." menu item?
Ahh, i deleted my wrong comment. Checked out function call tree what happens when you click on list item title. See screenshot: If you press "Edit in..." it calls "STSNavigateWithCheckoutAlert" from core.js

Other Answer3

Create a feature that adds a SPItemEventReceiver to the library and override ItemUploded . Or use this tool to add events to the lybrary.


The document needs to be changed on open and different per user. So don't think this can work.

Other Answer4

Would it be possible to use a custom page, load the document and process it before sending it to the client?

So that the question would me more like "how to make all relevant links point to this redirecting page instead of directly to the document?"

Which of course may be impossible, particularly in case of working with documents via WebDAV

Other Answer5

HTTPHandler is the only place in the IIS/Asp.Net/SharePoint stack where you have the ability to modify documents that are being sent to the user. As for this being outside of SharePoint, it isn't really when you consider that HTTPHandlers are a part of Asp.Net and SharePoint is an Asp.Net application.

If you really wanted to, you could still package and deploy your code as a SharePoint Solution and even have the Feature's activation automatically change the web.config to activate your handler.