Dah Sra
Dah Sra

Reputation: 4425

Fetch all the Queries under a azure projects using REST call

I am trying to fetch all the available queries under a project using the below REST call https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/queries/list?view=azure-devops-rest-5.0#uri-parameters

It needs a depth parameter to be passed if not only the First level of queries are returned and it seems the maximum allowed value for depth is 2 .

If i have 3 levels of folder structure in queries even this depth wont help.

So how to retrieve all the queries irrespective of levels ?

TIA

Upvotes: 0

Views: 722

Answers (2)

starian chen-MSFT
starian chen-MSFT

Reputation: 33698

You also can do it with client API, simple code:

static void GetQueryClientAPI()
        {
            VssCredentials Credentials = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "Personal access token"));
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("devops url"), Credentials);
            tpc.EnsureAuthenticated();
            WorkItemStore wis = tpc.GetService(typeof(WorkItemStore)) as WorkItemStore;
            QueryHierarchy qh = wis.Projects["project name"].QueryHierarchy;
            foreach(QueryItem q in qh)
            {
                GetChildQuery(q);
            }
            Console.Read();
        }
        static void GetChildQuery(QueryItem query)
        {

            if (query is QueryFolder)
            {
                QueryFolder queryFolder = query as QueryFolder;
                foreach (var q in queryFolder)
                {
                    GetChildQuery(q);
                }
            }
            else
            {
                QueryDefinition querydef = query as QueryDefinition;
                Console.WriteLine(querydef.Name + " -- " + querydef.Path);
            }
        }

Result:

enter image description here

Upvotes: 0

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16018

As workaround you can use Microsoft.TeamFoundationServer.Client and explore queries structure with depth 1. Example:

    static void GetAllWorkItemQueries(string project)
    {
        List<QueryHierarchyItem> rootQueries = WitClient.GetQueriesAsync(project, QueryExpand.All).Result;

        GetFolderContent(project, rootQueries);
    }

    /// <summary>
    /// Get Content from Query Folders
    /// </summary>
    /// <param name="project">Team Project Name</param>
    /// <param name="queries">Folder List</param>
    static void GetFolderContent(string project, List<QueryHierarchyItem> queries)
    {
        foreach(QueryHierarchyItem query in queries)
        {
            if (query.IsFolder != null && (bool)query.IsFolder)
            {
                Console.WriteLine("Folder: " + query.Path);
                if ((bool)query.HasChildren)
                {
                    QueryHierarchyItem detiledQuery = WitClient.GetQueryAsync(project, query.Path, QueryExpand.All, 1).Result;
                    GetFolderContent(project, detiledQuery.Children.ToList());
                }
            }
            else
                Console.WriteLine("Query: " + query.Path);
        }
    }

Full sample project here: https://github.com/ashamrai/TFRestApi/tree/master/04.TFRestApiAppWorkItemQueries

Upvotes: 1

Related Questions