从Visual Studio Team Services REST API获取所有工作项

时间:2018-09-03 18:46:23

标签: .net tfs azure-devops

我每天需要获取5000个工作项,并且需要准备一些报告,但是REST API一次仅支持200个插槽,在这种情况下,我将对API进行多次调用。还有其他方法可以一次获取所有工作项吗?

1 个答案:

答案 0 :(得分:1)

您正在使用哪个版本的TFS?如果您使用的是本地TFS 2018或VSTS,则可以使用REST API-Reporting Work Item Revisions - Read Reporting Revisions GetReporting Work Item Revisions - Read Reporting Revisions Post来获取所有工作项:

GET https://{accountName}.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=4.1

您可以根据需要添加可选参数:

GET https://{accountName}.visualstudio.com/{project}/_apis/wit/reporting/workitemrevisions?fields={fields}&types={types}&continuationToken={continuationToken}&startDateTime={startDateTime}&includeIdentityRef={includeIdentityRef}&includeDeleted={includeDeleted}&includeTagRef={includeTagRef}&includeLatestOnly={includeLatestOnly}&$expand={$expand}&includeDiscussionChangesOnly={includeDiscussionChangesOnly}&$maxPageSize={$maxPageSize}&api-version=4.1

例如,我们可以使用以下PowerShell脚本从特定项目中检索所有工作项(您还可以将结果导出到*.csv文件中,然后在Excel中打开):

Param(
   [string]$baseurl = "http://server:8080/tfs/DefaultCollection", 
   [string]$projectName = "0511ScrumTFVC",
   [string]$user = "domain\user",
   [string]$token = "password/PAT"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "$baseurl/$($projectName)/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Clear-Host
Write-Host "Count of Work items"$result.values.count 
$wits = @()
foreach ($wit in $result.values)
{

$customObject = new-object PSObject -property @{
          "WitID" = $wit.fields.'System.id'
          "rev" = $wit.Rev
          "Title" = $wit.fields.'System.Title'
        } 

    $wits += $customObject
}

$wits | Select `
                WitID,
                rev, 
                Title #|export-csv -Path D:\temp\WITs.csv -NoTypeInformation

enter image description here