如何使用C#中的TFS rest API在现有用户故事下创建子任务?

时间:2018-02-15 09:15:21

标签: c# rest tfs azure-devops azure-devops-rest-api

我知道可以使用https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples中提到的TFS rest API在批量调用中创建用户素材和子任务。

但我的要求是我已经有了现有的用户故事,我想在c#中的现有用户故事下创建子任务。

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

请参阅此示例代码:

var url= new Uri("https://XXX.visualstudio.com");
                VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));
var connection = new VssConnection(url, c);
            var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();
            string projectName = "scrum2015";
            int parentWITId = 771;
            var patchDocument = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();
            patchDocument.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation() {
                Operation=Operation.Add,
                Path= "/fields/System.Title",
                Value="childWIT"
            });
            patchDocument.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/-",
                Value = new
                {
                    rel = "System.LinkTypes.Hierarchy-Reverse",
                    url = connection.Uri.AbsoluteUri+ projectName+ "/_apis/wit/workItems/"+parentWITId,
                    attributes = new
                    {
                        comment = "link parent WIT"
                    }
                }
            });
            var createResult = workitemClient.CreateWorkItemAsync(patchDocument, projectName, "task").Result;

答案 1 :(得分:0)

您可以使用此示例(只需将Bug更改为任务):https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples#create-bug

此外,您还必须添加有关链接的信息:https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#with-a-work-item-link 在你的情况下:

  

patchDocument [4] = new {op =&#34; add&#34;,path =&#34; / relations / - &#34;,value = new {rel =&#34; System.LinkTypes。 Hierarchy-Reverse&#34;,url =&#34; https://accountname.visualstudio.com/_apis/wit/workitems/ {USERSTORY_ID}&#34;}};