Gavin van Gent
Gavin van Gent

Reputation: 127

How to add an attachment to a work item in Visual Studio Online and C#

I'm having problems adding an attachment to a work item in Visual Studio Online, working from http://www.visualstudio.com/en-us/integrate/reference/reference-vso-work-item-work-items-vsi#UpdateworkitemsAddanattachment

I pull email messages from an inbox that I watch, and from that, create work items in Visual Studio Online. In this instance, I have found a reply to an email, and want to add history to the vso workitem:

public static bool AppendHistoryToTicket(int ticketID, Message mailMessage)
{
    StringBuilder historyEntry = new StringBuilder(1024);
    historyEntry.AppendLine(mailMessage.Sender.Address + " replied on " + mailMessage.DateTimeCreated.ToString("dd MMM yyyy HH:mm:ss") + " GMT - ");
    historyEntry.AppendLine("");
    historyEntry.AppendLine(mailMessage.Body.Content);

    var uri = TicketURI.UpdateWorkItem(ticketID);
    WebRequest request = WebRequest.CreateHttp(uri);
    request.ContentType = "application/json-patch+json; charset=utf-8";
    request.Method = "PATCH";
    request.Headers.Add("Authorization", Settings.UserAuthorization);

    PatchProperty[] properties = new PatchProperty[]{
        new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
        new PatchProperty("add", "/relations/-", new
        {
            rel = "AttachedFile",
            url = mailMessage.Attachment.URL,
            attributes = new
            {
                comment = "This is a test"
            }
        })
    };

    byte[] byte1 = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(properties));
    request.ContentLength = byte1.Length;

    using (Stream stream = request.GetRequestStream())
        stream.Write(byte1, 0, byte1.Length);

    Logger.Write("Updating the history of ticket: #" + ticketID);

    using (WebResponse response = request.GetResponse())
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
        JObject jsonObj = JObject.Parse(sr.ReadToEnd());
        Models.Ticket result = new Models.Ticket(JsonConvert.DeserializeObject<DTOs.Ticket>(jsonObj.ToString()));
        return result != null;
    }
}

If I do this, it works (obviously without adding the attachment):

PatchProperty[] properties = new PatchProperty[]{
    new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
};

but with:

PatchProperty[] properties = new PatchProperty[]{
    new PatchProperty("add", "/fields/System.History", historyEntry.ToString()),
    new PatchProperty("add", "/relations/-", new
    {
        rel = "AttachedFile",
        url = mailMessage.Attachment.URL,
        attributes = new
        {
            comment = "This is a test"
        }
    })
};

I get a 400 (Bad Request)

Any help would be appreciated

Upvotes: 2

Views: 2136

Answers (1)

Buck Hodges
Buck Hodges

Reputation: 3352

I believe what you want is the "Add a Hyperlink" call instead from the same MSDN page, as you are only looking to add a link (mailMessage.Attachment.URL) rather than upload a doc. That would mean rel would be "Hyperlink" instead and no comment.

Upvotes: 2

Related Questions