11

Does anyone know if it is possible to retrieve a list of work items and their linked work items in one trip from TFS using their TFS API web services?

At the moment, we are having to make a second call for each of the work items made during the first call, and is introducing a performace issue.

If that is not possibly, is there a way to peek at the type of the linked work item without retrieving them (e.g. See if it is a task or issue) ?

btlog
  • 4,760
  • 2
  • 29
  • 38
Prabu
  • 4,097
  • 5
  • 45
  • 66

2 Answers2

19

The article you 're referring to in your answer presents with a method to do what you 're after, using WIQL. Certainly, not a bad choice.

Another way, in my opinion better, is to simply generate graphically the query that yields the results you 're after. You probably need a simple "Work Items and Direct Link":
enter image description here

Once you 've saved that you 'll be able to:

  1. Open the query in VS & Team Web Access
  2. Tie the query with Excel & work on WIs from within Excel
  3. Catch the query results with TFS-API.

For the latter part, supposing your query is named "MyLinkedQuery" and it resides under "Team Queries" of TeamProject "MyProj", you can do something like this:

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

namespace LinkedQueryResults
{
    class Program
    {
        static void Main()
        {
            TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFSURL"));

            var workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));

            var project = workItemStore.Projects["MyProj"];
            QueryHierarchy queryHierarchy = project.QueryHierarchy;
            var queryFolder = queryHierarchy as QueryFolder;
            QueryItem queryItem = queryFolder["Team Queries"];
            queryFolder = queryItem as QueryFolder;

            if (queryFolder != null)
            {
                var myQuery = queryFolder["MyLinkedQuery"] as QueryDefinition;
                if (myQuery != null)
                {
                    var wiCollection = workItemStore.Query(myQuery.QueryText);
                    foreach (WorkItem workItem in wiCollection)
                    {
                        Console.WriteLine(workItem.Title); 
                    }
                }
            }       
        }
    }
}
pantelif
  • 8,524
  • 2
  • 33
  • 48
  • Are there any performance advantages to having the query saved? I also have items on a' third' layer that I need to retrieve, which I still haven't been able to figure how to. That is, Bugs linked to Test Cases, which are linked to the requirement work item. – Prabu May 28 '12 at 00:52
  • Retrieving the query from TFS should have, in fact, a performance penalty. This would be minimal though. The main gain is that you can visualize in VS the results & maintain it easily and consistently. The other option (having a hard-coded WIQL string within your console app) might prove on the long run to be less maintainable. – pantelif May 28 '12 at 07:53
  • The 'third' level you mention is not reachable from visually generated queries in VS, but you can certainly do it with C#. – pantelif May 28 '12 at 08:00
  • I've decided to go with WIQL hard coded inside the application, especially since I needed to access that third level of items. The reason why I wasn't able to before was because of the direction the items were linked. (a --> b <-- c) I was searching from a and could not find c, using a recursive query specifying the links must be in one direction. – Prabu Jun 01 '12 at 05:33
9

Found an article regarding this issue.

It allows you to use a tree query, where you can get the parent item ids and it's linked items ids in one query. Using this, a second query can be used to get the actual detailed work item objects. Two queries to solve the issue.

Edit: I also wrote a post about this on my blog.

BioGeek
  • 21,897
  • 23
  • 83
  • 145
Prabu
  • 4,097
  • 5
  • 45
  • 66