TFS API - How to fetch work item(s) from specific Team Project

61,708

Solution 1

It's probably most efficient to use a query to find the workitems you're interested in. You can add a Where project = '@Project' to the query to limit the scope to just that project. By first calling BeginQuery and then EndQuery you'll get a workitem collection for just the items you were looking for.

The easiest way to get the required wql query is to create a query in Team Explorer, then use file->save as (in edit mode) to save it to file. Open that file in Notepad to copy the query out of there.

Alternatively you can use the WorkItemStore.Query method directly to achieve the same thing.

Solution 2

You could try something like this for getting all WIs within teamProject:

WorkItemCollection workItemCollection = workItemStore.Query(
     " SELECT [System.Id], [System.WorkItemType],"+    
     " [System.State], [System.AssignedTo], [System.Title] "+ 
     " FROM WorkItems " +
     " WHERE [System.TeamProject] = '" + teamProject.Name +
    "' ORDER BY [System.WorkItemType], [System.Id]");

And this to get a specific WorkItem ID:

WorkItem workItem = workItemStore.GetWorkItem(555);
Share:
61,708
JF Beaulieu
Author by

JF Beaulieu

Full Stack C# .NET / Typescript / Javascript Web Developer Microsoft MCSD Web Applications certified developer Interested in modern web technologies such as: .NET Framework 4.7.2 C# 8.0 ASP.NET Core 3.1 ASP.NET MVC 5 Entity Framework 7 SQL Server 2016 RESTful Web Services WCF Services (SoA) jQuery React Angular AngularJS Bootstrap SignalR

Updated on February 18, 2020

Comments

  • JF Beaulieu
    JF Beaulieu about 4 years

    I am trying to query a single team project in the main TfsTeamProjectCollection which contains 194 Team Projects in total. I know exactly how to get a WorkItem by Id from a WorkItemStore. The thing is, that by doing this, the API searches in ALL of the projects in the collection and the query takes about a minute. This is way too slow, there must be a way to query work items directly from a single team project ? Here is the code I have:

        private Uri collectionUri;
        private TfsTeamProjectCollection projectCollection;
        private WorkItemStore workItemStore;
    
        public Project GetTeamProject()
        {
            projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUri);
    
            workItemStore = projectCollection.GetService<WorkItemStore>();
            Project teamProject = workItemStore.Projects[TFS_PROJECT_KEY];
            return teamProject;
        }
    

    Now that I have the Team Project I'm interested in, how can I query for work items by ID or just get all work items in this project ?

  • JF Beaulieu
    JF Beaulieu about 12 years
    Do you know what reference to add to the project for the query to be recognized? Because I already am using Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.WorkItemTracking.Client and I get errors for everything in parameter of workItemStore.Query()
  • jessehouwing
    jessehouwing about 12 years
    Note that, just as with SQL Queries, you can use @ParameterName to add parameters to your Team queries. Just add a Dictionary as last parameter with the name/value pairs and the WorkItemStore will make sure the data is properly escaped. (see also: msdn.microsoft.com/en-US/library/bb140400(v=vs.80).aspx)
  • DaveF
    DaveF over 11 years
    The saving of the wql query is a brilliant tip. Much easier to get your query right using Team Explorer GUI. You've saved me hours of messing about :-)
  • dyslexicanaboko
    dyslexicanaboko over 10 years
    I am just throwing it out there, if you want a specific work item you have to filter for it like so: " AND [System.Id] = " + workItemID; - that wasn't explicitly stated above, no biggy.