Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

tfs

tfs-sdk

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 ?

like image 491
JF Beaulieu Avatar asked Feb 13 '12 19:02

JF Beaulieu


People also ask

What is WIQL query?

You can use the WIQL syntax to define a query as a hyperlink or when using the Work Item Query Language (REST API). The WIQL syntax supports all functions available through the web portal Query Editor plus a few more. You can specify the fields to return and specify logical grouping of query clauses.

How do I query work items in Azure DevOps?

From your web browser, open Boards>Queries. To open, view, and run queries in Visual Studio 2019, you need to Set the Work Items experience to the legacy option. From the Team Explorer home page, choose Work Items.

How do I access TFS API?

Accessing TFS/VSTS through the REST API To setup the personal access token, login to TFS/VSTS and go to your profile (icon in the right top corner), then click on the security link. You can then have access to view, add and expire tokens.


2 Answers

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); 
like image 80
pantelif Avatar answered Sep 26 '22 21:09

pantelif


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.

like image 45
jessehouwing Avatar answered Sep 22 '22 21:09

jessehouwing