使用 Team Foundation 的客户端对象模型创建工作项

你可以通过执行以下步骤创建 bug、任务以及其他类型的 WorkItem

  1. 构造 WorkItem

  2. 设置必填字段的值。

  3. 保存 WorkItem

示例

根据你创建的 WorkItem 的类型,大部分必填的 Fields 都有默认值。 如果这些值合适,你无需显式设置它们。 例如,你可以创建如 适用于 Visual Studio ALM 的 MSF for Agile Software Development 中所定义的用户情景。 对于此类 WorkItem,“状态”、“原因”和“指派给”Fields 全部是必填字段,但是具有默认值。 在创建用户情景时,其默认状态是“活动”,其默认原因是“新建”,“指派给”字段的默认值是当前用户。 但是,标题为必填,并且没有默认值。 因此,你必须在创建用户情景时设置标题。 有关详细信息,请参阅用户情景(敏捷)[重定向]可在 Visual Studio TFS 中配置并自定义的内容的端到端视图。 以下示例创建用户情景、设置标题(必填)并设置说明(非必填)。

用户情景

使用此示例

  1. 创建 C#(或 VB)控制台应用程序。

  2. 添加对下列程序集的引用:

  3. 将 Program.cs(或 Module1.vb)的内容替换为此示例。

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("User Story")

        ' 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

备注

通过使用 WorkItemStore.BatchSave 方法,你可以在单个往返过程中保存多个 WorkItemWorkItemLink

请参见

任务

使用 Team Foundation 的客户端对象模型编辑和保存工作项

参考

BatchSave

概念

使用 Team Foundation 的客户端对象模型扩展工作项跟踪

使用 Team Foundation 的客户端对象模型为不同类型的工作项编写代码