If you have already created the UserStory and Task, then you just need to call the updateWorkItem()
method to link the user story and child tasks with the following json patch doc (update Task in this sample). See Work Items - Update for details.
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "UserStoryUrl",
"attributes": {
"isLocked": false,
"comment": "Link to parent user story",
"name": "Parent"
}
}
}
You can also link the parent User Story when creating tasks, just get the User Story URL and integrate it to the json patch doc. Something like below:
[
{
"op": "add",
"path": "/fields/System.Title",
"value": "Test"
},
{
"op": "add",
"path": "/relations/-",
"value": {
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "User Strory URL here",
"attributes": {
"isLocked": false,
"comment": "Link to User Story as the parent work item",
"name": "Parent"
}
}
}
]
Reference this thread : How to create work items using Visual Studio Team Services Client for Node.js (vso-node-api)?
async function createUS() {
let wijson: vss.JsonPatchDocument = [{ "op": "add", "path": "/fields/System.Title", "value": "Task created from Node JS" }];
let project: string = "Project";
let witype: string = "User Story";
let usWI: wi.WorkItem = await vstsWI.createWorkItem(null, wijson, project, witype);
console.log(usWI.id);
}
createUS();
async function createTask() {
let wijson: vss.JsonPatchDocument = [{ "op": "add", "path": "/fields/System.Title", "value": "Task created from Node JS" },{"op":"add","path":"/relations/-","value":{"rel":"System.LinkTypes.Hierarchy-Reverse","url":"usWI.url","attributes":{"isLocked":false,"comment":"Link to parent User Story","name":"Parent"}}}];
let project: string = "Project";
let witype: string = "Task";
let taskWI: wi.WorkItem = await vstsWI.createWorkItem(null, wijson, project, witype);
console.log(taskWI.id);
}
createTask();