0
votes

I am trying to update a field(/fields/System.Description) of a work-item on TFS using the TFS API according to the official Microsoft document. There is something wrong with the api "Update work items" when using the sample code listed on the page, the returning status code of the response is 500 if I want to "add" or "replace" a certain filed, but success if I want to "test" a value, can anyone please give me some help?

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

using Newtonsoft.Json;

public static void UpdateWorkItemUpdateField()
    {
        string _personalAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
        string _id = "111";

        Object[] patchDocument = new Object[1];

        patchDocument[0] = new { op = "replace", path = "/fields/System.Description", value = "Changing the description done" };


        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

            var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json"); // mediaType needs to be application/json-patch+json for a patch call
            var method = new HttpMethod("PATCH");
            var request = new HttpRequestMessage(method, "http://mysite:8080/tfs/MyCollection/_apis/wit/workitems/" + _id + "?api-version=1.0") { Content = patchValue };
            var response = client.SendAsync(request).Result;

            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
            }
        }
    }

and here is the return status:

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Pragma: no-cache X-TFS-ProcessId: xxxxxxxxxxxxxxxx X-FRAME-OPTIONS: SAMEORIGIN X-VSS-UserData: xxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxx ActivityId: xxxxxxxxxxxxxxxxxxxxxxxxxxxx X-TFS-Session: xxxxxxxxxxxxxxxxx Lfs-Authenticate: NTLM X-Content-Type-Options: nosniff Cache-Control: no-cache Date: Thu, 01 Jun 2017 06:50:04 GMT P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT" Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 428 Content-Type: application/json; charset=utf-8 Expires: -1}}

1
I tested the code you posted above, it's correct. Can you find some other error logs in Event viewer? There're many reasons caused the 500 error. Did you enable Basic Authentication on your TFS server? Are you using TFS 2017? - Tingting0929
@Tingting0929-MSFT The status code is 200 at present, but I did nothing today. I don't know why it worked without any operation, but thanks for your kind help these days. I may need to test more to locate the possible problem. - RickyXRQ

1 Answers

0
votes

I tried to make the same request but only without the MyCollection part in the url and it worked.

try to do it yourself. Danny