Search code examples
c#tfsworkitemkanban

C# Get TFS Kanban Board Column for each Work Item


I have my WorkItemStore collection. I need to do an action on each WorkItem within. While I am iterating through I need to get the BoardColumn it is in. Pretty simple, but I am having a hard time figuring out how to get the WorkItem to tell me it's 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
        }

Any help is greatly appreciated!!!!


Solution

  • The easiest way would be to upgrade your TFS to TFS 2015 Update 1 and later, as in TFS 2015.1 and later version, the Board Column field has been enabled in work item queries. You can easily query and display of Kanban fields in a work item query.

    To get TFS Board Column field for each Work Item in a query, please refer to the code snippet below:

    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);
                }
    
            }
        }
    }