azure-devopsazure-devops-rest-apitfs-workitemwiql

how to get past n interations of workitems from azure devops rest api


I need to pull the current iterations and past n iterations of workitems with specific fields dynamically.

To explain you in detail about Past n iterations. Lets say my n is 5, then i would need workitems from current iteration, past iteration to current iteration and past to past to current iteration.

In total i have more than 400 iterations out of which i will need to pull latest past 5 iteration workitems.

To fetch the workitems with specific fields from current iteration, this is the powershell script i have written

$token = 'your PAT'
$url="https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
   "query": "SELECT [System.Id], [System.WorkItemType],  [System.State],[System.AreaPath],[System.Tags],[System.CommentCount],[System.ChangedDate] 
                   FROM workitems 
              Where [System.AreaPath] = 'area path' 
                 and [System.IterationPath] = 'iteration path' 
                 and [System.WorkItemType] = 'Story' 
              order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
$listOfTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
  $listOfTasks.Add($workitem.id)
}
$listOfTasks = $listOfTasks -join ','
$listOfTasks
$JSON1 = @"
{
  
  "ids": [$listOfTasks],
  "fields": [
    "System.Id",
    "System.Title",
    "System.WorkItemType",
    "System.IterationPath",
  ]      
}
"@
$JSON1
$url1="https://dev.azure.com/Organizationname/_apis/wit/workitemsbatch?api-version=5.0"
$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON1 -ContentType application/json
Write-Host "result = $($response1 | ConvertTo-Json -Depth 100)"

Can someone please help me in tweaking this logic, so that i can get current and past n iterations workitems. Many thanks in advance!!


Solution

  • Past and current iterations are in the team settings. So, you have to know the team name to get the iteration list. Then:

    1. In the team settings you can get iterations: Iterations - List

    https://dev.azure.com/{organization}/{project}/{team}/_apis/work/teamsettings/iterations?api-version=6.1-preview.1

    1. Each iteration contains dates and the timeframe (current, future, past).
    2. You can use an iteration from the list in your source code or use Iterations - Get Iteration Work Items to get iteration work items:

    https://dev.azure.com/{organization}/{project}/{team}/_apis/work/teamsettings/iterations/{iterationId}/workitems?api-version=6.1-preview.1

    Example to get last 5 past iteration:

    $user = ""
    $token = "<PAT>"
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    $org = "<ORG_NAME>"
    $teamProject = "<PROJECT_NAME>"
    $teamName = "<TEAM_NAME>"
    
    $restApiGetIterations = "https://dev.azure.com/$org/$teamProject/$teamName/_apis/work/teamsettings/iterations?api-version=6.1-preview.1"
    
    $restApiGetIterations
    
    function InvokeGetRequest ($GetUrl)
    {   
        return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    }
    
    
    $iterations = InvokeGetRequest $restApiGetIterations
    
    $pastIterations = $iterations.value | Where-Object {$_.attributes.timeFrame -eq "past"} 
    
    $past5Iterations = $pastIterations | Sort-Object -Property {$_.attributes.finishDate} | Select-Object -Last 5
    
    
    $past5Iterations