我正在尝试使用以下REST调用来获取项目下的所有可用查询 https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/queries/list?view=azure-devops-rest-5.0#uri-parameters
如果不仅返回第一级查询,而且似乎允许的最大深度值为2,则需要传递一个depth参数。
如果我在查询中具有3个级别的文件夹结构,即使深度也不行。
那么如何检索所有查询而与级别无关?
TIA
答案 0 :(得分:1)
作为解决方法,您可以使用Microsoft.TeamFoundationServer.Client并探索深度为1的查询结构。示例:
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);
}
}
此处有完整的示例项目:https://github.com/ashamrai/TFRestApi/tree/master/04.TFRestApiAppWorkItemQueries
答案 1 :(得分:1)
您也可以使用客户端API(简单的代码)完成
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);
}
}
结果: