I am working on a program to access the REST API for Visual Studio Team Services (was Visual Studio Online). I follow https://www.visualstudio.com/integrate/api/wit/work-items
I managed to request a work item by passing the correct id using this piece of code:
var uri = new Uri("https://{instance}.visualstudio.com/DefaultCollection/_apis/wit/workitems/7?api-version=1.0"); GetWorkItem(uri); public static async void GetWorkItem(Uri uri) { try { var username = "my username"; var password = " my pass word"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", username, password)))); using (HttpResponseMessage response = client.GetAsync(uri) .Result) { response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } Console.Read(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.Read(); } }
It correctly returns JSON as indicated here https://www.visualstudio.com/integrate/api/wit/work-items#GetalistofworkitemsByIDs
Now I am trying to update a work item by changing its title.
https://www.visualstudio.com/integrate/api/wit/work-items#UpdateworkitemsUpdateafield
For this, I wrote a method:
public static async void UpdateWorkItemStatus(Uri requestUri, HttpContent iContent) { { var method = new HttpMethod("PATCH"); var request = new HttpRequestMessage(method, requestUri) { Content = iContent }; HttpResponseMessage response; try { using (HttpClient client = new HttpClient()) { var username = "my username"; var password = "my password"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", username, password)))); response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); Console.WriteLine(response); Console.Read(); } } catch (TaskCanceledException e) { Console.WriteLine("ERROR: " + e.ToString()); Console.Read(); } } }
I call this method passing my json:
var uri = new Uri("https://{instance}.visualstudio.com/DefaultCollection/_apis/wit/workitems/7?api-version=1.0"); string json = new JavaScriptSerializer().Serialize(new { op="replace", path="fields/System.Title", value=" 123 New Title" }); HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json-patch+json"); UpdateWorkItemStatus(uri, httpContent);
This is consistent with the information at https://www.visualstudio.com/integrate/api/wit/work-items#Updateworkitems
They do not have code samples, so I used the JavascriptSerializer, but it does nothing. The code works, but does not output the result, and my work item is also not editable. I'm not sure if this is incorrect in the format due to the use of the JavascriptSerializer, but I used this class before and it worked well.
Basically I need to pass this JSON:
[ { "op": "replace", "path": "fields/System.Title", "value":"New Title" } ]
Any help on how to accomplish this and pass the JSON in the correct format will be appreciated, even if without using the JS Serializer class.
Ultimately, the idea is to convert this into an interpreted script that can work on Unix, such as curl, Python, or Perl. Any pointers or recommendations on this will also be appreciated.