public void WorkItemTracking_WorkItems_CreateAndLinkMultipleWorkItems_Success() { // arrange Batch request = new Batch(_configuration); // act WorkItemBatchPostResponse response = request.CreateAndLinkMultipleWorkItems(_configuration.Project); // assert foreach (WorkItemBatchPostResponse.Value value in response.values) { Assert.AreEqual(200, value.code); WorkItemPatchResponse.WorkItem workitem = JsonConvert.DeserializeObject <WorkItemPatchResponse.WorkItem>(value.body); Assert.IsTrue(workitem.relations.Length == 1); } request = null; }
// <summary> // Create work item and link the multiple work items // </summary> // <param name="projectName"></param> // <returns>WorkItemPatchResponse.WorkItem</returns> public WorkItemBatchPostResponse CreateAndLinkMultipleWorkItems(string projectName) { WorkItemBatchPost.BatchRequest[] batchRequests = new WorkItemBatchPost.BatchRequest[2]; Dictionary <string, string> headers = new Dictionary <string, string>() { { "Content-Type", "application/json-patch+json" } }; Object[] parentPatchDocumentBody = new Object[2]; parentPatchDocumentBody[0] = new { op = "add", path = "/fields/System.Title", value = "Customer can sign in using their Microsoft Account" }; parentPatchDocumentBody[1] = new { op = "add", path = "/id", value = "-1" }; batchRequests[0] = new WorkItemBatchPost.BatchRequest { method = "PATCH", uri = '/' + projectName + "/_apis/wit/workitems/$User Story?api-version=2.2", headers = headers, body = parentPatchDocumentBody }; Object[] childPatchDocumentBody = new Object[3]; childPatchDocumentBody[0] = new { op = "add", path = "/fields/System.Title", value = "JavaScript implementation for Microsoft Account" }; childPatchDocumentBody[1] = new { op = "add", path = "/id", value = "-2" }; childPatchDocumentBody[2] = new { op = "add", path = "/relations/-", value = new { rel = "System.LinkTypes.Hierarchy-Reverse", url = _configuration.UriString + "_apis/wit/workitems/-1" } }; batchRequests[1] = new WorkItemBatchPost.BatchRequest { method = "PATCH", uri = '/' + projectName + "/_apis/wit/workitems/$Task?api-version=2.2", headers = headers, body = childPatchDocumentBody }; 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 batchRequest = new StringContent(JsonConvert.SerializeObject(batchRequests), Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/$batch?api-version=2.2") { Content = batchRequest }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { var stringResponse = response.Content.ReadAsStringAsync(); WorkItemBatchPostResponse batchResponse = response.Content.ReadAsAsync <WorkItemBatchPostResponse>().Result; return(batchResponse); } } return(null); }