Team Foundation のクライアント オブジェクト モデルを使用した作業項目の作成

次の手順を行うと、バグ、タスク、その他の種類の 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 メソッドを使用すると、1 回のラウンド トリップで複数の WorkItem または WorkItemLink を保存できます。

参照

処理手順

Team Foundation のクライアント オブジェクト モデルを使用した作業項目の編集と保存

関連項目

BatchSave

概念

Team Foundation のクライアント オブジェクト モデルを使用した作業項目トラッキングの拡張

Team Foundation のクライアント オブジェクト モデルを使用したさまざまな種類の作業項目のコードの記述