Freigeben über


Sample Code: Create a Work Item in Team Foundation

You can create bugs, tasks, and other types of WorkItems by performing the following steps:

  1. Construct a WorkItem.
  2. Set the values of the required fields.
  3. Save the WorkItem.

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 MSF for Agile Software Development v5.0. 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 Customizing 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

 

  1. Create a console application.

  2. Add references to the following assemblies:

    • Microsoft.TeamFoundation
    • Microsoft.TeamFoundation.Client
    • Microsoft.TeamFoundation.WorkItemTracking.Client
  3. Replace the contents of Program.cs with this example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WorkItemTrackingSample2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Connect to the server and the store and get the WorkItemType object
            // for user story from the team project where the user story will be created.
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https:// server:8080/tfs/DefaultCollection"));
            WorkItemStore workItemStore = (WorkItemStore)tpc.GetService(typeof(WorkItemStore));
            Project teamProject = workItemStore.Projects["DinnerNow"];
            WorkItemType wit = teamProject.WorkItemTypes["User Story"];

            // Create the work item.
            WorkItem userStory = new WorkItem(wit);

            // The title is the only required field that does not have a default value.
            // You must set it, or you cannot save the work item.
            userStory.Title = "Recently ordered menu";
           
            // Also set the description.
            userStory.Description= "As a return customer, I want to see items from the menu that I've recently ordered.";

            // Save the new user story.
            userStory.Save();
        }
    }
}