Share via


使用 Team Foundation 的用戶端物件模型來建立工作項目

您可以執行下列步驟來建立 Bug、工作和其他類型的 WorkItem

  1. 建構 WorkItem

  2. 設定必要欄位的值。

  3. 儲存 WorkItem

範例

根據所建立 WorkItem 的類型,大部分必要 Fields 都有預設值。 如果這些值適用,則不需要明確地設定它們。 例如,您可以建立 MSF for Agile Software Development (適用於 Visual Studio ALM) 中所定義的使用者劇本。 針對這類型的 WorkItem,狀態、原因和指派給 Fields 都是必要的項目,但都有預設值。 建立使用者劇本時,其預設狀態為 [使用中]、其預設原因是 [新增],而 [指派給] 欄位的預設值是目前使用者。 不過,標題是必要項目,而且沒有預設值。 因此,您必須在建立使用者劇本時設定標題。 如需詳細資訊,請參閱使用者劇本 (Agile) [重新導向]您可以在 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("http://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 的用戶端物件模型,為不同的工作項目類型撰寫程式碼