by Ewald Hofman
10. December 2009 00:31
In Part 1 I have described how to access the Application Instance of the TFS server. This post describes how you can create new work items, including a new linked work item. The new link makes use of the new link type in TFS 2010, so it shows up on the correct tab page. In this example a new User Story is created with one Task as its implementation and one Test Case which tests the User Story:
In order to get access to the work items, add a reference to:
- Microsoft.TeamFoundation.WorkItemTracking.Client.dll
You can find the dll’s in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0
Then you have to add the following using statements:
using Microsoft.TeamFoundation.WorkItemTracking.Client;
You can now create the work items with the following code:
// Get the work item store
WorkItemStore wiStore = tfai.GetTeamFoundationServer(tpc.Id).GetService<WorkItemStore>();
// Get the team project
var project = wiStore.Projects["Agile"];
// Create a new User Story
var wiUserStory = new WorkItem(project.WorkItemTypes["User Story"])
{
Title = string.Format("New User Story, created at {0:g}", DateTime.Now),
Description = "This is an example how you create a new work item with the SDK"
};
wiUserStory.Save();
// Create a new Task
var wiTask = new WorkItem(project.WorkItemTypes["Task"])
{
Title = "Create a very secure design"
};
wiTask.Save();
// Add a parent-child link between User Story and Task
var hierarchicalLink = wiStore.WorkItemLinkTypes["System.LinkTypes.Hierarchy"];
wiUserStory.WorkItemLinks.Add(new WorkItemLink(hierarchicalLink.ForwardEnd, wiTask.Id));
wiUserStory.Save();
// Create a new Test Case
var wiTestCase = new WorkItem(project.WorkItemTypes["Test Case"])
{
Title = "Test on security",
};
wiTestCase.Fields["Steps"].Value = "<steps id=\"0\" last=\"2\"><step id=\"1\" type=\"ActionStep\"><parameterizedString>" +
"<text>Go to the correct url</text></parameterizedString><parameterizedString /><description />" +
"</step><step id=\"2\" type=\"ActionStep\"><parameterizedString><text>Hack the site</text>" +
"</parameterizedString><parameterizedString /><description /></step></steps>";
wiTestCase.Save();
// Add a tested by link between User Story and Test Case
var testedByLink = wiStore.WorkItemLinkTypes["Microsoft.VSTS.Common.TestedBy"];
wiUserStory.WorkItemLinks.Add(new WorkItemLink(testedByLink.ForwardEnd, wiTestCase.Id));
wiUserStory.Save();