TFS SDK 2010 – Part 4 - Create a new User Story, with the implementation (Tasks) and test scenarios (Test Case)

by Ewald Hofman 10. December 2009 08: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"

// Create a new Task
var wiTask = new WorkItem(project.WorkItemTypes["Task"])
    Title = "Create a very secure design"

// 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));

// 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>";

// 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));




12/11/2009 1:13:49 AM #

Clemens Reijnen

Ewald,  in the namespace Microsoft.TeamFoundation.TestManagement.Client you can find more testcase / teststep related functionality, wil make the creation of testcase and teststeps easier [not that xml]. I think you can cast the wi to Itestcase... Used it for the testcase generation

Clemens Reijnen Netherlands |

12/11/2009 2:18:23 AM #

Ewald Hofman

I will create a seperate blog post on this topic soon then. Thanks for the notification!

Ewald Hofman Netherlands |

11/27/2010 5:38:45 AM #


Hi Ewald -

I have got as far as creating a WPF app where i am able to retrive the team project list and project artificats such as team queries. The query results are displayed in a Grid (on which i further apply rules before display). All is good when i am running flat queries. But when i try and achive the same for linked or tree queries i fail with the following error message.

TF248021: You have specified a query string that is not valid when you use the query method for a flat list of work items. You cannot specify a parameterized query or a query string for linked work items with the query method you specified.

I have tried a bit but can't get around running a tree query and display the results in a grid. Any help appreciated!

Pasting my code if that helps,            
            QueryDefinition q_def = (QueryDefinition)item.Tag;
            string q = q_def.QueryText;
            txt_query.Text = q;

            dataGrid1.ItemsSource = Store.Query
                (q.Replace("@project", "'" + ProjInfo.Name + "'").
                Replace("@Me", "'" + Environment.UserName + "'"));

Walkman United Kingdom |

Comments are closed

Powered by BlogEngine.NET
Theme by Mads Kristensen



Statistics created at 09 Sep 2009

121 posts
328 raters
1952506 visit (1042 per day)
19 users online

Recent comments

Comment RSS