C# 获取每个工作项的 TFS 看板列

C# Get TFS Kanban Board Column for each Work Item

我有我的 WorkItemStore 集合。我需要对其中的每个 WorkItem 执行操作。当我迭代时,我需要获取它所在的 BoardColumn。非常简单,但我很难弄清楚如何让 WorkItem 告诉我它是 BoardColumn。

        // Get our WorkItem Store
        TfsTeamProjectCollection projectCollection = GetTfsProjectCollection(tfsCollectionUri);
        WorkItemStore workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));

        // Run a query for all Tasks on the "New Creative Work" Board.
        WorkItemCollection queryResults = workItemStore.Query(
            "Select [State], [Title] " +
            "From WorkItems " +
            "Where[System.AreaPath] = '<MySysAreaPath>' " +
            "AND[System.BoardLane] = '<MyBoardLane>'" +
            "AND[System.State] Does Not Contain 'Completed'");

        foreach(WorkItem item in queryResults)
        {
            string myBoardColumn = item[DO NOT KNOW WHAT GOES HERE]...
            //DO Stuff > Update db record
        }

非常感谢任何帮助!!!!

最简单的方法是将 TFS 升级到 TFS 2015 Update 1 及更高版本,因为在 TFS 2015.1 及更高版本中,Board Column 字段已在工作项查询中启用。您可以在工作项查询中轻松查询和显示看板字段。

要获取查询中每个工作项的 TFS Board Column 字段,请参考下面的代码片段:

using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;
using System;

namespace GetWorkItemField
{
    class Program
    {
        static void Main(string[] args)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfsserver:8080/tfs/teamprojectcollection"));
            var service = tfs.GetService<WorkItemStore>();
            string workItemQueryString = "Select Id, Title From WorkItems Where [System.TeamProject] = 'TeamProject'";
            var workItemQuery = new Query(service, workItemQueryString);
            WorkItemCollection queryResults = workItemQuery.RunQuery();

            foreach (WorkItem item in queryResults)
            {
                var t = item.Fields["System.BoardColumn"];
                Console.WriteLine("{0}: {1}", item.Title, t.Value);
            }

        }
    }
}