Search code examples
c#tfsazure-devopstfs-sdkwiql

TFS API Returns only first level folders C#


Im trying to implement a TFS API to fetch all work items. Its establishing the connection without any trouble. But the issue Im facing is its only fetching the fist levels of folders. Actually the fodler I am looking for is inside those folders. Somewhere deep inside 4th level. Here is the code Im trying

        string collectionUri = ConfigurationManager.AppSettings["tfsPath"].ToString();//http://myserver:8080/tfs/defaultcollection
        string teamProjectName = ConfigurationManager.AppSettings["tfsProject"];//mycompany

         VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

        // Create instance of WorkItemTrackingHttpClient using VssConnection
        WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

        List<QueryHierarchyItem> queryHierarchyItems = witClient.GetQueriesAsync(teamProjectName, depth:2).Result;

        foreach(QueryHierarchyItem qh in queryHierarchyItems )
        {
            string s = qh.Name;    
        }
        // Search for 'Special Queries' folder
        QueryHierarchyItem myQueriesFolder = queryHierarchyItems.FirstOrDefault(qhi => qhi.Name.Equals("Special Queries"));

Here queryHierarchyItems is always null. I tried using that for loop above and I found its not getting into second levels of folders. So how can I accomplish my requirement or what I did wrong


Solution

  • If you know the path to the query folder use:

    var folder = witClient.GetQueryAsync(teamProject, path, depth: 1).Result;
    

    Then you can access the queries in that folder using something like:

    var queries = folder.Children.Where(x => !x.IsFolder.GetValueOrDefault());