Create a Work Item By Using the Client Object Model for Team Foundation
You can create bugs, tasks, and other types of WorkItems by performing the following steps:
Example
Depending on the type of WorkItem that you create, most required Fields have default values. If these values are appropriate, you do not have to set them explicitly. For example, you might create a user story as defined in Agile Process Template for Visual Studio ALM. For this type of WorkItem, the State, Reason, and Assigned to Fields are all required but have default values. When a user story is created, its default state is "Active," its default reason is "New," and the default value of the Assigned to field is the current user. However, the title is required and has no default value. Therefore, you must set the title when you create a user story. For more information, see User Story (Agile) and Customize Team Projects and Processes. The following example creates a user story; sets the title, which is required; and sets the description, which is not required.
To use this example
Create a C# ( or VB ) console application.
Add references to the following assemblies:
Replace the contents of Program.cs ( or Module1.vb ) with this example.
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace WorkItemTrackingSample
{
class Program
{
static void Main(string[] args)
{ // Connect to the server and the store, and get the WorkItemType object
// for user stories from the team project where the user story will be created.
Uri collectionUri = (args.Length < 1) ?
new Uri("http://server:port/vdir/DefaultCollection") : new Uri(args[0]);
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri);
WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects["DinnerNow"];
WorkItemType workItemType = teamProject.WorkItemTypes["User Story"];
// Create the work item.
WorkItem userStory = new WorkItem(workItemType)
{
// The title is generally the only required field that doesn’t have a default value.
// You must set it, or you can’t save the work item. If you’re working with another
// type of work item, there may be other fields that you’ll have to set.
Title = "Recently ordered menu",
Description =
"As a return customer, I want to see items that I've recently ordered."
};
// Save the new user story.
userStory.Save();
}
}
}
Imports System
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Module Module1
Sub Main(ByVal sArgs() As String)
' Connect to the server and the store and get the WorkItemType object
' for user stories from the team project where the user story will be created.
Dim collectionUri As Uri
If sArgs.Length = 0 Then
collectionUri = New Uri("https://Server:8080/tfs/DefaultCollection")
Else
collectionUri = New Uri(sArgs(1))
End If
Dim tpc As New TfsTeamProjectCollection(collectionUri)
Dim workItemStore As WorkItemStore
workItemStore = tpc.GetService(Of WorkItemStore)()
Dim teamProject As Project
teamProject = workItemStore.Projects("DinnerNow")
Dim workItemType As WorkItemType
workItemType = teamProject.WorkItemTypes("UserTypes")
' Create the work item
Dim userStory As New Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem(workItemType)
' The title is generally the only required field that doesn’t have a default value.
' You must set it, or you can’t save the work item. If you’re working with another
' type of work item, there may be other fields that you’ll have to set.
userStory.Title = "Recently Ordered Menu"
userStory.Description = "As a return customer, I want to see items that I've recently ordered"
' Save the new user story
userStory.Save()
End Sub
End Module
Note
You can save more than one WorkItem or WorkItemLink in a single round trip by using the WorkItemStore.BatchSave method.
See Also
Tasks
Edit and Save Work Items by Using the Client Object Model for Team Foundation
Reference
Concepts
Extending Work Item Tracking by Using the Client Object Model for Team Foundation
Writing Code for Different Types of Work Items by Using the Client Object Model for Team Foundation