如何获取Azure DevOps中三级及以上深度的查询及文件夹信息?
解决Azure DevOps查询深层文件夹内容的方案
方案1:修改API请求的$depth参数为all
你之前用的$depth=2只会返回两级内容,其实Azure DevOps的WIT查询API支持将$depth设为all,递归返回所有层级的文件夹和查询。调整后的API链接:
https://dev.azure.com/{Organization}/{Project}/_apis/wit/queries?$depth=all&api-version=7.1-preview.2
直接调用这个链接,就能一次性拿到三级及以上的所有查询内容。
方案2:递归遍历文件夹(备用方案)
如果$depth=all因环境限制无法生效,可以通过递归调用API逐个遍历文件夹:
PowerShell示例脚本
$orgName = "你的组织名" $projectName = "你的项目名" $pat = "你的PAT令牌" $baseUri = "https://dev.azure.com/$orgName/$projectName/_apis/wit/queries" # 递归获取所有查询的函数 function Get-AllQueries { param( [string]$folderUrl ) $headers = @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))} $response = Invoke-RestMethod -Uri "$folderUrl?api-version=7.1-preview.2" -Headers $headers -Method Get # 收集当前文件夹下的查询 $queries = $response.value | Where-Object { $_.isFolder -eq $false } | Select-Object name, id, url # 递归处理子文件夹 foreach ($folder in $response.value | Where-Object { $_.isFolder -eq $true }) { $queries += Get-AllQueries -folderUrl $folder.url } return $queries } # 从根文件夹(My Queries/Shared Queries)开始遍历 $headers = @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))} $rootResponse = Invoke-RestMethod -Uri "$baseUri?api-version=7.1-preview.2" -Headers $headers -Method Get $allQueries = @() foreach ($rootFolder in $rootResponse.value) { $allQueries += Get-AllQueries -folderUrl $rootFolder.url } # 输出结果表格 $allQueries | Format-Table -AutoSize
这个脚本会从根目录开始,逐层递归遍历所有子文件夹,自动收集所有符合要求的查询属性。
方案3:使用Azure DevOps CLI
如果已安装Azure DevOps CLI,用命令可以更简洁地获取结果:
az devops login az boards query list --project "你的项目名" --depth all --organization "https://dev.azure.com/你的组织名" --output table --query "[].{Name:name, ID:id, URL:url}"
该命令会直接输出包含name、id、url的结构化表格,自动处理所有层级的文件夹。
内容的提问来源于stack exchange,提问作者user23314406
相关产品推荐
相关产品推荐

