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

可以通过执行下列步骤创建bug、任务和 WorkItem中的其他类型:

  1. 构造 WorkItem

  2. 设置必填字段的值。

  3. 保存 WorkItem

示例

根据您创建 WorkItem 的类型,大多数需的 Fields 的默认值为。如果这些值是正确的,因此您不需要显式设置这些属性。例如,可以创建用户情景如 适用于 Visual Studio ALM 的敏捷过程模板定义。对于这种类型 WorkItem,并分配到 Fields 都会需要状态,原因,但保留默认值。当用户情景创建时,其默认状态为“活动”,其默认原因为“新的,”,且分配字段的默认值是当前用户。但是,需要标题并没有默认值。因此,那么,当您创建用户情景时,必须设置标题。有关更多信息,请参见用户情景 (Agile)自定义团队项目和过程。下面的示例创建一个用户情景;设置标题,需要;并将声明,则不需要。

用户情景

使用此示例

  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 the only required field that does not have a default value. 
                // You must set it, or you cannot save the work item. 
                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 the only required field that does not have a default value
        ' You must set it, or you cannot save the item
        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 的客户端对象模型为不同类型的工作项编写代码