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

さまざまなチーム プロジェクトのコードを書き込む場合、同様の目的を果たす作業項目のさまざまなタイプの同じ機能を実行できます。これらの型はさまざまなプロセス テンプレートから先、またはチームは、特定のチーム プロジェクトに対してをカスタマイズする可能性がある異なる場合があります。たとえば、どの顧客が要求および評価するかを表すする条件実行する場合があります、ユーザーにストーリーの機能を同じ。作業項目の各タイプの執筆別のコードではなく、両方で働く対してコードを一般化できます。さらに、1 種類チームは、そのチーム メンバーがストーリー ポイントの代わりに時間の作業を見積もるできるように、ユーザーのストーリーの作業項目定義をカスタマイズする場合があります。既定値とカスタマイズされた工程の両方を処理するために、コードを調整すること自分の激しさをコピーすることを回避できます。

このトピックでは、特定のタイプのカスタマイズをサポートする方法をカスタマイズされていない、リファクターにそのコードを参照して作業項目の特定のタイプのタスクを実行このオプションを移動する。作業項目の型をカスタマイズする方法の詳細については チーム プロジェクトとプロセスのカスタマイズを参照してください。

このトピックの内容

  • ストーリー ポイントの見積とのユーザーのストーリーのツリーを印刷します。

  • ストーリー ポイントまたはベースラインの作業の見積とのユーザーのストーリーのツリーを印刷します。

  • 見積との条件として使用する作業項目のツリーを印刷します。

ストーリー ポイントの見積とのユーザーのストーリーのツリーを印刷します。

このこのオプションは、各チーム サーバーのプロジェクトのすべてのユーザーにストーリーのツリーを印刷し、子をいない各ストーリーにの見積が含まれます。このオプションを使用するには、アプリケーション コンソールを作成し、次のアセンブリへの参照を追加して次のコードと Program.cs (または) Module1.vb の内容を置き換えます。

using System;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Microsoft.TeamFoundation.SDK
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Connect to Team Foundation Server. The form of the url is http://server:port/vpath.
                //     Server - the name of the server that is running the application tier for Team Foundation.
                //     port - the port that Team Foundation uses. The default port is 8080.
                //     vpath - the virtual path to the Team Foundation application. The default path is tfs.
                TfsConfigurationServer configurationServer =
                    TfsConfigurationServerFactory.GetConfigurationServer(new Uri("https://Server:8080/tfs"));
               
                // Get the catalog of team project collections
                CatalogNode catalogNode = configurationServer.CatalogNode;
                ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(
                    new Guid[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);

                // Process each team project collection
                foreach (CatalogNode tpcNode in tpcNodes)
                {
                    // Use the InstanceId property to get the team project collection
                    Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
                    TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);

                    // Get the work item store
                    WorkItemStore wiStore = tpc.GetService<WorkItemStore>();

                    // Query for the trees of active user stories in the team project collection
                    StringBuilder queryString = new StringBuilder("SELECT [System.Id] FROM WorkItemLinks WHERE ");
                    queryString.Append("([Source].[System.WorkItemType] = 'User Story' AND [Source].[System.State] = 'Active') AND ");
                    queryString.Append("([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ");
                    queryString.Append("([Target].[System.WorkItemType] = 'User Story' AND [Target].[System.State] = 'Active') ORDER BY [System.Id] mode(Recursive)");
                    Query wiQuery = new Query(wiStore, queryString.ToString());
                    WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery();

                    // Print the trees of user stories, with the estimated sizes of each leaf
                    PrintTrees(wiStore, wiTrees, "    ", 0, 0);
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        // Each WorkItemLinkInfo structure in the collection contains the IDs of the linked work items.
        // In this case, the sourceId is the ID of the user story that is on the parent side of the link, and
        // the targetId is the ID of the user story that is on the child side of the link. The links
        // are returned in depth-first order. This function recursively traverses the collection
        // and the title of each user story. If the user story has no children, its estimation is also printed.
        static int PrintTrees(WorkItemStore wiStore, WorkItemLinkInfo[] wiTrees, string prefix, int sourceId, int iThis)
        {
            int iNext = 0;

            // Get the parent of this user story, if it has one
            WorkItem source = null;
            if (sourceId != 0)
            {
                source = wiStore.GetWorkItem(wiTrees[iThis].SourceId);
            }

            // Process the items in the list that have the same parent as this user story
            while (iThis < wiTrees.Length && wiTrees[iThis].SourceId == sourceId)
            {
                // Get this user story
                WorkItem target = wiStore.GetWorkItem(wiTrees[iThis].TargetId);
                Console.Write(prefix);
                Console.Write(target.Type.Name);
                Console.Write(": ");
                Console.Write(target.Fields["Title"].Value);
                if (iThis < wiTrees.Length - 1)
                {
                    if (wiTrees[iThis].TargetId == wiTrees[iThis + 1].SourceId)
                    {
                        // The next item is this user story's child. Process the children
                        Console.WriteLine();
                        iNext = PrintTrees(wiStore, wiTrees, prefix + "    ", wiTrees[iThis + 1].SourceId, iThis + 1);
                    }
                    else
                    {
                        // The next item is not this user story's child.
                        Console.Write("; estimate = ");
                        Console.WriteLine(target.Fields["Story Points"].Value);
                        iNext = iThis + 1;
                    }
                }
                else
                {
                    // This user story is the last one.
                    iNext = iThis + 1;
                }

                iThis = iNext;
            }

            return iNext;
        }
    }
}
Imports System
Imports System.Text
Imports System.Collections.ObjectModel
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.Framework.Client
Imports Microsoft.TeamFoundation.Framework.Common
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Module Module1
    Sub Main(ByVal sArgs() As String)

        Try
            ' Connect to the Team Foundation Server. The form of the url is http://server:port/vpath.
            ' Server - the name of the server that is running the application tier for Team Foundation
            ' Port - the port that Team Foundation uses. The default port is 8080.
            ' Vpath - the virtual path to the Team Foundation application. The default is tfs.

            Dim tfsUri As New Uri("https://Server:8080/tfs")
            Dim configurationServer As TfsConfigurationServer
            configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri)

            ' Get the catalog of team project collections
            Dim catalogNode As CatalogNode
            catalogNode = configurationServer.CatalogNode
            Dim tpcNodes As ReadOnlyCollection(Of CatalogNode)
            Dim gVar As Guid() = New Guid() {CatalogResourceTypes.ProjectCollection}
            tpcNodes = catalogNode.QueryChildren(gVar, False, CatalogQueryOptions.None)
            ' Process each team project collection
            For Each tpcNode In tpcNodes
                Dim tpcGuid As Guid = New Guid(tpcNode.Resource.Properties("InstanceID"))
                Dim tpc As New TfsTeamProjectCollection(tfsUri)
                tpc = configurationServer.GetTeamProjectCollection(tpcGuid)
                ' Get the work item store
                Dim wiStore As WorkItemStore
                wiStore = tpc.GetService(Of WorkItemStore)()
                ' Query for the trees of active user stories in the team project collection
                Dim queryString As New StringBuilder("SELECT [System.ID] FROM WorkItemLinks WHERE ")
                queryString.Append("([Source].[System.WorkItemType] = 'User Story' AND [Source].[System.State] = 'Active') AND ")
                queryString.Append("([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ")
                queryString.Append("([Target].[System.WorkItemType] = 'User Story' AND [Target].[System.State] = 'Active') ORDER BY [System.Id] mode(Recursive)")
                Dim wiQuery As New Query(wiStore, queryString.ToString())
                Dim wiTrees() As WorkItemLinkInfo
                wiTrees = wiQuery.RunLinkQuery()

                ' Print the trees of user stories with the estimated size of each leaf.
                PrintTrees(wiStore, wiTrees, "   ", 0, 0)
            Next

        Catch ex As Exception
            Console.WriteLine(ex.Message)

        End Try
        


    End Sub

    ' Each WorkItemLinkInfo structure in the collection contains the IDs of the linked work items.
    ' In this case, the sourceId is the ID of the user story that is on the parent side of the link, and
    ' the targetId is the ID of the user story that is on the child side of the link. The links
    ' are returned in depth-first order. This function recursively traverses the collection
    ' and the title of each user story. If the user story has no children, its estimation is also printed.

    Function PrintTrees(ByVal wiStore As WorkItemStore, ByVal wiTrees As WorkItemLinkInfo(), ByVal prefix As String, ByVal sourceId As Integer, ByVal iThis As Integer) As Integer

        Dim iNext As Integer = 0

        ' Get the parent of this user story, if it has one
        Dim source As WorkItem = Nothing

        If sourceId <> 0 Then
            source = wiStore.GetWorkItem(wiTrees(iThis).SourceId)
        End If

        ' Process the items in the list that have the same parent as this user story
        While (iThis < wiTrees.Length AndAlso wiTrees(iThis).SourceId = sourceId)
            ' Get this user story

            Dim target As WorkItem
            target = wiStore.GetWorkItem(wiTrees(iThis).TargetId)
            Console.Write(prefix)
            Console.Write(target.Type.Name)
            Console.Write(":  ")
            Console.Write(target.Fields("Title").Value)
            If iThis < (wiTrees.Length - 1) Then
                If wiTrees(iThis).TargetId = wiTrees(iThis + 1).SourceId Then

                    ' The next item is the user story's child.
                    Console.WriteLine()
                    iNext = PrintTrees(wiStore, wiTrees, prefix + "   ", wiTrees(iThis + 1).SourceId, iThis + 1)
                Else

                    ' The next item is not the user story's child
                    Console.Write("; estimate = ")
                    Console.WriteLine(target.Fields("Story Points").Value)
                    iNext = iThis + 1
                End If
            Else
                ' This user story is the last one.
                iNext = iThis + 1
            End If
            iThis = iNext
        End While
        Return iNext
    End Function

End Module

ストーリー ポイントまたはベースラインの作業の見積とのユーザーのストーリーのツリーを印刷します。

このオプションは PrintTrees 方法各作業項目がストーリー ポイントをフィールドについて使用するまたはベースラインのフィールドが決定され、そのフィールドの見積を変更するかどうかを印刷します。このオプションを使用するには、次のコードと PrintTrees 方法の明細行 Console.WriteLine(target.Fields["Story Points"].Value); を置き換えます。

// Determine which estimation field is present
string fieldName = "Story Points";
if (target.Type.FieldDefinitions.TryGetByName(fieldName) == null) 
{
    fieldName = "Baseline Work";
}
Console.WriteLine(target.Fields[fieldName].Value); 
Dim fieldName As String
fieldName = "Story Points"
If (target.Type.FieldDefinitions.TryGetByName(fieldName) Is Nothing) Then
    fieldName = "Baseline Work"
End If
Console.WriteLine(target.Fields(fieldName).Value)

カテゴリのすべての作業項目のツリーの印刷

カテゴリに異なるさまざまなチーム プロジェクトの同じまたは同様の目的を果たす作業項目の型をグループ化できます。カテゴリに属するすべての作業項目の同じ機能を実行するコードを作成できます。このオプションは、ユーザーのストーリー、条件、プロダクト受注残の品目のような条件のカテゴリの作業項目のすべての型の見積を印刷します。

このオプションを使用するには、次のコードと前のサンプルから主機能を置き換えます。

static void Main(string[] args) 
{
    try
    {
        // Connect to Team Foundation Server. The form of the url is http://server:port/vpath.
        //     server - the name of the server that is running the application tier for Team Foundation.
        //     port - the port that Team Foundation uses. The default ort is 8080.
        //     vpath - the virtual path to the Team Foundation application. The default path is tfs. 
        TfsConfigurationServer configurationServer =
            TfsConfigurationServerFactory.GetConfigurationServer(new Uri("https://server:8080/tfs"));

        // Get the catalog of team project collections
        CatalogNode catalogNode = configurationServer.CatalogNode; 
        ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(
            new Guid[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None); 

        // Process each team project collection
        foreach (CatalogNode tpcNode in tpcNodes) 
        {
            // Use the InstanceId property to get the team project collection
            Guid tpcId = new Guid(tpcNode.Resource.Properties["InstanceId"]);
            TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId); 

            // Get the work item store
            WorkItemStore wiStore = tpc.GetService<WorkItemStore>();
            foreach (Project project in wiStore.Projects)
            {
                Console.Write("Project: ");
                Console.WriteLine(project.Name); 

                // Get the type of work item to use
                CategoryCollection categories = wiStore.Projects[project.Name].Categories;
                string wiType = categories["Requirement Category"].DefaultWorkItemType.Name;

                // Query for the trees of active user stories in the team project collection
                StringBuilder queryString = new StringBuilder("SELECT [System.Id] FROM WorkItemLinks WHERE ");
                queryString.Append("([Source].[System.WorkItemType] = '");
                queryString.Append(wiType);
                queryString.Append("' AND [Source].[System.TeamProject] = '");
                queryString.Append(project.Name);
                queryString.Append("') AND ");
                queryString.Append("([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ");
                queryString.Append("([Target].[System.WorkItemType] = 'User Story'  AND  ");
                queryString.Append("[Target].[System.State] = 'Active') ORDER BY [System.Id] mode(Recursive)"); 
                Query wiQuery = new Query(wiStore, queryString.ToString());
                WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery();

                // Print the trees of user stories and requirements, with the estimated size of each leaf

                PrintTrees(wiStore, wiTrees, "    ", 0, 0); 
            }
        }
    }

    catch (Exception e) 
    {
        Console.WriteLine(e.Message); 
    }
}
Sub Main(ByVal sArgs() As String)

        Try
            ' Connect to the Team Foundation Server. The form of the url is http://server:port/vpath.
            ' Server - the name of the server that is running the application tier for Team Foundation
            ' Port - the port that Team Foundation uses. The default port is 8080.
            ' Vpath - the virtual path to the Team Foundation application. The default is tfs.

            Dim tfsUri As New Uri("https://server:8080/tfs")
            Dim configurationServer As TfsConfigurationServer
            configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri)

            ' Get the catalog of team project collections
            Dim catalogNode As CatalogNode
            catalogNode = configurationServer.CatalogNode
            Dim tpcNodes As ReadOnlyCollection(Of CatalogNode)
            Dim gVar As Guid() = New Guid() {CatalogResourceTypes.ProjectCollection}
            tpcNodes = catalogNode.QueryChildren(gVar, False, CatalogQueryOptions.None)
            ' Process each team project collection
            For Each tpcNode In tpcNodes
                Dim tpcGuid As Guid = New Guid(tpcNode.Resource.Properties("InstanceID"))
                Dim tpc As New TfsTeamProjectCollection(tfsUri)
                tpc = configurationServer.GetTeamProjectCollection(tpcGuid)
                ' Get the work item store

                Dim wiStore As WorkItemStore
                wiStore = tpc.GetService(Of WorkItemStore)()
                ' Query for the trees of active user stories in the team project collection
                Dim project As Project
                For Each project In wiStore.Projects
                    Console.Write("Project: ")
                    Console.WriteLine(project.Name)

                    ' Get the type of work item to use
                    Dim categories As CategoryCollection
                    categories = wiStore.Projects(project.Name).Categories
                    Dim wiType As String
                    wiType = categories("Requirement Category").DefaultWorkItemType.Name
                    ' Query for the trees of active user stories in the team project collection
                    Dim queryString As New StringBuilder("SELECT [System.Id] FROM WorkItemLinks WHERE ")
                    queryString.Append("([Source].[System.WorkItemType] = '")
                    queryString.Append(wiType)
                    queryString.Append("' AND [Source].[System.TeamProject] = '")
                    queryString.Append(project.Name)
                    queryString.Append("') AND ")
                    queryString.Append("([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ")
                    queryString.Append("([Target].[System.WorkItemType] = 'User Story' AND [Target].[System.State] = 'Active') ORDER BY [System.Id] mode(Recursive)")
                    Dim wiQuery As New Query(wiStore, queryString.ToString())
                    Dim wiTrees() As WorkItemLinkInfo
                    wiTrees = wiQuery.RunLinkQuery()

                    ' Print the trees of user stories with the estimated size of each leaf.
                    PrintTrees(wiStore, wiTrees, "   ", 0, 0)
                Next
                
            Next

        Catch ex As Exception
            Console.WriteLine(ex.Message)

        End Try
    End Sub