Wednesday, November 27, 2019

How to fetch all projects from AzureDevOps and list it in Excel?

In this blog I am going explain how you can export the list of all projects of an Azure Devops organistaions to an excel for reporting purposes:

The document referred is: https://docs.microsoft.com/en-us/rest/api/azure/devops/core/projects/list?view=azure-devops-rest-5.1

Use the below power shell code and execute it:

#Enter the organisation name
$vstsaccount="Enter Organisation Name"
#Enter the API
$api="https://dev.azure.com/$($vstsaccount)/_apis/projects?`$top=200&api-version=5.1"
#Enter the user id
$user="arun.varriar@orgname.com"
#Enter a PAT which has org level access
$token="Enter org access PAT"

$base64AuthInfo=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Invoke the API
$result = Invoke-RestMethod -Uri $api -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} 

#Write the result to excel
$result | select -Expand value | ForEach {
        $_.name = $_.name -join ' '
        $_
    } | Export-Csv C:\MyFolder\list.csv -NoTypeInformation

Here in API the filter criteria we have used is $top=200, so it will list only the top 200 projects in ADO(done with an assumption that we have projects less than 200, so all the projects will be listed).

So without using the $top if you want to list all the projects you need to use Continuation Token which is going to be the heart of API calls. To get the list of all projects using ContinuationToken use the below script:

$vstsaccount=""
$projects=$null
$user=""
$token="pat"
$base64AuthInfo=[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$ContinuationToken=$null
$j=$true
while($j  -eq $true)
{
$projectname="https://dev.azure.com/$($vstsaccount)/_apis/projects?ContinuationToken=$ContinuationToken&api-version=5.1"
$result = Invoke-WebRequest -Uri $projectname -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} 
$continuationToken = $result.Headers.'x-ms-continuationtoken'
$projectsets =$result.Content | ConvertFrom-Json
$projects += $projectsets.value.name
$projectsets | select -Expand value | ForEach {
        $_.name = $_.name -join ' '
        $_
    } | Export-Csv C:\MyFolder\list.csv -NoTypeInformation -Append
    if($ContinuationToken -eq $null)
    {
    $j=$false
    }

}


You will have an excel with list all projects in the path specified. ENJOY!!!

1 comment: