How to create child tasks under existing user stor

2019-08-30 08:56发布

问题:

I know there is a provision for creating user story and a child task in batch call by using TFS rest API as mentioned in https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples.

But my requirement is I already have existing user story and I want to create child tasks under existing user story in c#.

Does anybody is having an idea how to do this?

回答1:

Refer to this sample code:

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;


回答2:

You may use this example (just change Bug to Task): https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples#create-bug

Also you have to add information about link: https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#with-a-work-item-link in your case:

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