Search code examples
c#tfsazure-devopswiql

how to find all the Sprint's bugs,Feature,Task work in TFS by WIQL C# code?


i want to find the Query to find all the bugs,Task,Feature,Stories of all the sprints of both my team.
i have 2 team in my Project name "MRI_Scrum_GIt" which cantains 2 team Phoneix and SS and these 2 teams contains there Respected sprints(which is going on or done)
and how to get all the Teams invloved in the Project(Query) in my case SS and phoniex of MRI_Scrum

enter image description here


Solution

  • I've created query with group operation instead operation "In":

    enter image description here

    For my project wiql in saved query is:

    SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM WorkItemLinks WHERE ([Source].[System.TeamProject] = 'VSTSScrum' AND ( [Source].[System.WorkItemType] = 'Bug' OR [Source].[System.WorkItemType] = 'Product Backlog Item' OR [Source].[System.WorkItemType] = 'Feature' ) AND ( [Source].[System.IterationPath] UNDER 'VSTSScrum\SS' OR [Source].[System.IterationPath] UNDER 'VSTSScrum\Phoneix' )) And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ([Target].[System.WorkItemType] = 'Task' OR [Target].[System.WorkItemType] = 'Product Backlog Item') ORDER BY [System.Id] mode(Recursive)

    Source code for your request:

    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace QueryLinkedWIQL
    {
        class Program
        {
            static void Main(string[] args)
            {
                WorkItemStore _wistore = new WorkItemStore("http://server/collection");
    
                string _teamProject = "VSTSScrum";
                string _teamPhoneixRootIteration = "VSTSScrum\\Phoneix";
                string _teamSSRootIteration = "VSTSScrum\\SS";
    
                    string _wiql = string.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.TeamProject] = '{0}'"+ 
                    "AND ( [Source].[System.WorkItemType] = 'Bug'  OR  [Source].[System.WorkItemType] = 'Product Backlog Item'  OR  [Source].[System.WorkItemType] = 'Feature' ) " + 
                    "AND ( [Source].[System.IterationPath] UNDER '{1}'  OR  [Source].[System.IterationPath] UNDER '{2}' )) " + 
                    "And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And " + 
                    "([Target].[System.WorkItemType] = 'Task'  OR  [Target].[System.WorkItemType] = 'Product Backlog Item') ORDER BY [System.Id] mode(Recursive)",
                    _teamProject, _teamPhoneixRootIteration, _teamSSRootIteration);
    
                Query _query = new Query(_wistore, _wiql);
    
                WorkItemLinkInfo[] _links = _query.RunLinkQuery();
    
                foreach(WorkItemLinkInfo _link in _links)
                {
                    //process link info item ....
                }
            }
        }
    }
    

    ==============================

    If you do not know all your teams. You can find them like here:Add teams and team members

    In this case all your teams have to have default iteration "ProjectName\IterationName".

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace QueryLinkedWIQL
    {
        class Program
        {
            static List<string> ListTeams(TfsTeamProjectCollection pTpc, Project pProject)
            {
                TfsTeamService _teamService = pTpc.GetService<TfsTeamService>();
                var _teams = _teamService.QueryTeams(pProject.Uri.ToString());
                return (from t in _teams select t.Name).ToList();
            }
    
            static string ConstructTeamsString(string pProjectName, List<string> pTeamNames)
            {
                string _val = "";
    
                for(int  i = 0; i< pTeamNames.Count; i++)
                    if (pTeamNames[i] == "SS" || pTeamNames[i] == "Phoneix") // I have many teams without iteration root name. So I use this if to remove unneeded teams. You can remove this line
                        _val += ((_val != "") ? " OR " : "") + string.Format("[Source].[System.IterationPath] UNDER '{0}\\{1}'", pProjectName, pTeamNames[i]);
    
                return _val;
            }
    
            static void Main(string[] args)
            {
                string _teamProject = "VSTSScrum";
    
                TfsTeamProjectCollection _tpc = new TfsTeamProjectCollection(new Uri("http://server/collection"));
    
                WorkItemStore _wistore = _tpc.GetService<WorkItemStore>();
    
                string _teamsStr = ConstructTeamsString(_teamProject, ListTeams(_tpc, _wistore.Projects[_teamProject]));
    
                string _wiql = string.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.TeamProject] = '{0}'"+ 
                    "AND ( [Source].[System.WorkItemType] = 'Bug'  OR  [Source].[System.WorkItemType] = 'Product Backlog Item'  OR  [Source].[System.WorkItemType] = 'Feature' ) " + 
                    "AND ( {1} )) " + 
                    "And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And " + 
                    "([Target].[System.WorkItemType] = 'Task'  OR  [Target].[System.WorkItemType] = 'Product Backlog Item') ORDER BY [System.Id] mode(Recursive)",
                    _teamProject, _teamsStr);
    
                Query _query = new Query(_wistore, _wiql);
    
                WorkItemLinkInfo[] _links = _query.RunLinkQuery();
    
                foreach(WorkItemLinkInfo _link in _links)
                {
                    //process link info item ....
                }
            }
        }
    }