First you need a reference to the team project collection:
var tfs = new TfsTeamProjectCollection(new Uri("https://your.tfspreview.com"));
tfs.EnsureAuthenticated();
var workItemStore = tfs.GetService<WorkItemStore>();
The the new task work item must be created. To instanciate a work item you must pass its type. You get this from the Project class.
var project = workItemStore.Projects["NameOfYourTeamProject"];
var taskType = project.WorkItemTypes["Task"];
var task = new WorkItem(taskType);
task.Title = "A new task";
task.State = "To Do";
task.Save();
Next, we need to create a parent / child link. We need a WorkItemLinkTypeEnd instance that we can get from the WorkItemLinkType that we can get from the work item store like this:
var linkType = workItemStore.WorkItemLinkTypes["System.LinkTypes.Hierarchy"];
var taskLink = new WorkItemLink(linkType.ReverseEnd, task.Id);
The taskLink instance is a parent / child work item link whose reverse end (where it points to aka the child) points to the id of the task work item we created formerly. To connect the close end to the parent, the backlog item, we have to load it and put the link into its Links collection:
var backlogItem = workItemStore.GetWorkItem(backlogItemId);´
backlogItem.Links.Add(taskLink);
backlogItem.Save();
Thats it - the new task appears as a child of the backlog item (for example in the taskboard). Hope this may save someone some time :-)
Keine Kommentare:
Kommentar veröffentlichen