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}}
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