Saving attachments in a workitem

This is one of pain points in V1 that we could not fix at end of project. To alleviate it, in the powertoys we do have an option to save attachments using commandline. I heard requests on saving attachments from UI so in this post I attached an addin (John's contribution) that will download attachments in a given workitem. Below is some info on this issue.

In attachments control, there is a button to open attachments and it effectively does shellexecute on attachment's URI link, and opens up in a browser. If the browser can show the attachment (such as images, htm/xml files) then it shows in browser window, otherwise invokes appropriate application. The file can be saved to disk from brower or other app by using File/Save menu. But there is no built-in functionality in UI to save an attachment without opening it. This is troublesome if someone wants to a) download large attachments or b) download files like encoded files or invalid xml items. Ideally a save button would stream contents to disk without letting browser get in.

First, why we chose to open attachments using webbrowser? It is because the files can be downloaded asynchronously without locking the UI (sometimes with support to continue previous downloads etc), and appropriate security errors are shown automatically. We did not have time to implement all these ourselves. How did we miss "Save" feature? Well, initially we had content-disposition for streaming attachments set to "attachment", and that forced a open/save dialog and life was good. But because of few bugs in a SP for browser that showed up later, this didn't work properly and had to remove that content-disposition, and it was too late to add a Save feature and am unhappy that we couldn't get a better user experience.

It is quite easy to save an attachment programmatically. If you have an attachment object, you can download attachment using its Uri property with these lines below:

            WebClient webClient = new WebClient();
            webClient.Credentials = CredentialCache.DefaultCredentials;
            webClient.DownloadFile(attachment.Uri.AbsoluteUri, destnationPathHere);

The attached addin uses similar code and integrates with workitem UI. So it can also be used as a sample to access workitem documents from an addin.

<%
//This posting is provided "AS IS" with no warranties of any kind and confers no rights.  Use of samples included in this posting is subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.
%>

SaveWitAttachments.zip