Request for a saved / saved TFS request using the full path and name of the request (requests are not included in My Requests or General Requests)

I am trying to access saved TFS requests in their full path (deeper than My Requests / General Requests) and the name of the request.

The scenario is that users can add the path to their command directory, and the request names of their work items can be added to the xml configuration file. I read xml and get data.

XML example

<Teams> <Team name ="MyTeam"> <Query project="MyProj" queryfolder="Shared Queries/blah/blah2/MyTeamFolder" queryName="MyTeams WorkItems - All Workitems"/> </Team> </Teams> 

I am looking to use "queryfolder" and "queryName" to find a query in TFS. What I still have is that it works for root directories ("My queries" / "General queries"), but I can’t figure out how getting something works on deeper paths.

This does not work:

  QueryHierarchy queryRoot = workItemStore.Projects["MyProj"].QueryHierarchy; QueryFolder folder = (QueryFolder)queryRoot["Shared Queries/blah/blah2/MyTeamFolder"]; QueryDefinition query = (QueryDefinition)folder["MyTeams WorkItems - All Workitems"]; string queryResults = query.QueryText; 

This will result in an error when searching for the folder QueryFolder folder = (QueryFolder) queryRoot [".."]

with KeyNotFoundException

changing a folder using "general queries" or "My queries", which can find this folder, but then would get the same exception when trying to find a query by its "name". And, as expected, if the request is in the root folder (general / my requests), it works fine.

So, how do I search deeper than just the root folder?

(all internet searches so far have cited only examples that use the root folder)

+4
source share
3 answers

This self-recursive method will find your query from the query tree, if one exists:

  static QueryDefinition GetQueryDefinitionFromPath(QueryFolder folder, string path) { return folder.Select<QueryItem, QueryDefinition>(item => { return item.Path == path ? item as QueryDefinition : item is QueryFolder ? GetQueryDefinitionFromPath(item as QueryFolder, path) : null; }) .FirstOrDefault(item => item != null); } 

Your example is specified, call

  var myquery = GetQueryDefinitionFromPath( (QueryFolder)workItemStore.Projects["MyProj"].QueryHierarchy, "Shared Queries/blah/blah2/MyTeamFolder/MyTeams WorkItems - All Workitems" ); 
+3
source

You can analyze the full folder path to a set of subfolders, and then create a loop to "move down" in the folder tree. You get an instance of QueryFolder for the next subfolder and assign it to the same variable until you reach the last.

Here is the implementation of this idea that I used in my project:

 string folderPath = "Shared Queries/blah/blah2/MyTeamFolder"; string currentSubfolder = folderPath.IndexOf("/") > 0 ? folderPath.Substring(0, folderPath.IndexOf("/")) : folderPath; QueryFolder queryFolder = (QueryFolder)workItemStore.Projects["MyProj"].QueryHierarchy[currentSubfolder]; while (folderPath.IndexOf("/") > 0) { folderPath = folderPath.Substring(folderPath.IndexOf("/") + 1); currentSubfolder = folderPath.IndexOf("/") > 0 ? folderPath.Substring(0, folderPath.IndexOf("/")) : folderPath; queryFolder = (QueryFolder)queryFolder[currentSubfolder]; } 

After that, you can use the request folder variable to work with requests within it.

0
source

The recursive answer above may work, but I had problems with it due to the fact that item.Path includes the entire path from the root and the path that I went through did not; which leads to a zero value as the final result. I also found that it does a lot of extra work since it touches every folder throughout the tree on the way to the folder you need.

I chose a simple drill process specifically designed for nodes that I knew would / should be there, making the method much more efficient, ignoring all the chaff. I pass the full path from the root and use string.Split ('/'). Skip (1) .ToArray () to get past the root node, avoiding KeyNotFoundException, since the root does not include itself. I intentionally did not handle the case when the folder was not found, since we are completely in control of what we are transferring and should only provide a valid path.

The code:

  private static QueryFolder GetQueryFolderFromPath(QueryFolder folder, string path) { var pathNodes = path.Split('/').Skip(1).ToArray(); foreach(var node in pathNodes) { folder = (QueryFolder)folder[node]; if (folder != null && folder.Path.Equals(path)) break; } return folder; } 
0
source

All Articles