Search code examples
tfswiql

WIQL query to get all item under a workitem


I am looking for a query where i can return all work items and their relation from a area path.

for example : project 1

i need all Featured all Userstories mapped to it all workitem and Bug mapped to userstories,

in short if i took a bug from the object i need somthing like parent id where i can match with the userstory.

string query1 = " SELECT * FROM WorkItemLinks " +
                    " WHERE ( [System.IterationPath] Under 'iteration1' )" +
                    " AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward' )" +
                    " ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";

which throwing an error like below

An exception of type 'Microsoft.TeamFoundation.WorkItemTracking.Client.ValidationException' occurred in Microsoft.TeamFoundation.WorkItemTracking.Client.dll but was not handled in user code

Additional information: TF51005: The query references a field that does not exist. The error is caused by «[System.IterationPath]».

when i checked the dll's are refereed correctly and when i re wrote the query like this the query is working fine

 string query = " SELECT * FROM WorkItems"+
                           " WHERE  ( [System.IterationPath] Under 'iteration1' )" +
                           //" AND  ([System.State] = 'Active' OR  [System.State] = 'Assessed' ) "+
                           //" AND  ( [Microsoft.VSTS.Scheduling.StartDate] <= '09/13/2017' AND  [Microsoft.VSTS.Scheduling.FinishDate] >= '09/13/2017' )"+
                           " ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";

but this query result does not give the relation ship that if a work item mapped as a child to other i need parent id in the work item object. how to get that. Thanks in advance.


Solution

  • You can try below query:

    Install Nuget Package Microsoft.TeamFoundationServer.ExtendedClient for the project.

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    using System;
    
    namespace _0925_WIQL
    {
        class Program
        {
            static void Main(string[] args)
            {
                TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
                      new Uri("http://server:8080/tfs/CollectionLC"));
                WorkItemStore workItemStore = (WorkItemStore)tpc.GetService(typeof(WorkItemStore));
    
    
                string query1= " SELECT * FROM WorkItemLinks " +
                        " WHERE ( Source.[System.IterationPath] Under 'TeamProject\\Iteration 1' )" +
                        " AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward' )" +
                        " ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";
    
                Query query = new Query(workItemStore, query1);
                WorkItemLinkInfo[] witLinkInfos = query.RunLinkQuery();
    
                foreach (WorkItemLinkInfo witinfo in witLinkInfos)
                {
    
                    .......
                }
    

    Besides, you can also use Wiql Editor. If you want to get all the parent workitems (IDs) from a specific child work item (ID), you can use below WIQL:

    SELECT                      
            [System.Id],
            [System.WorkItemType],
            [System.Title],
            [System.AssignedTo],
            [System.State]
    FROM workitemLinks
    WHERE ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward')
    AND ([Target].[System.Id] = 25)
    ORDER BY [System.Id]
    MODE (Recursive, ReturnMatchingChildren)