Search code examples
c#visual-studio-2015tfstfs-2015

Find which WorkItems are Code Reviews using the TFS API


Given a list of WorkItems, I would like to find the instances that correspond to a Code Review using the TFS 2015 API in C#.

While I can see from the text of the Description whether a WorkItem is a Code Review, I would prefer avoid parsing that string and rely on something more robust (eg: WorkItem.Type)...

How would you do that (the Type value seems quite cryptic to me)?


Solution

  • You could use the method below to get a workitem's type. But you need to provide the workitem id.

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    
            Uri collectionUri = (args.Length < 1) ?
                new Uri("http://servername:8080/tfs/MyCollection") : new Uri(args[0]);
    
            // Connect to the server and the store. 
            TfsTeamProjectCollection teamProjectCollection =
               new TfsTeamProjectCollection(collectionUri);
    
             WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
            string queryString = "Select [State], [Title],[Description] From WorkItems Where [Work Item Type] = 'Code Review Request' or [Work Item Type] = 'Code Review Response'";
    
            // Create and run the query.
            Query query = new Query(workItemStore, queryString);
            WorkItemCollection witCollection = query.RunQuery();
    
            foreach (WorkItem workItem in witCollection)
            {
                workItem.Open();
                Console.WriteLine(workItem.Fields["Title"].Value.ToString());
    
            }