Sonntag, 16. September 2012

Creating a child task for a backlog item with TFS API

Because TFS Express lacks the agile planning tools of the full version, I was writing a small tool that makes creating tasks as children of a backlog item more enjoyable. It took me a while to figure it out, so I share my result.

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