Shubham Tiwari
Shubham Tiwari

Reputation: 1171

Fetching a list of projects for a user in Azure DevOps

I am trying to fetch the list of projects for a specific user.Upon my research I have found 2 ways of doing this

  1. Using the OAuth Mechanism and then using the token to fetch a list of projects that an authenticated user is part of.(https://dev.azure.com/{Organization_Name}/_apis/projects?api-version=5.1) and it works fine but the problem is with my use case that I want to show the list of projects in a drop down and when ever I follow this approach I will have to authorize myself on the authorization screen.So is there a way to by pass that ?

  2. I have also found a way by which we can create PATs (Personal access tokens) and then call the Azure DevOps APIs but I haven't been able to find an endpoint that can return list of projects for a "Speciifc" user, so does anyone have any Idea how this can be achieved ?

  3. My last question is since the authentication in my application is backed by Azure AD can I use the AD token on Azure DevOps APIs ?

Just to give you an idea I am developing a .Net Core 3.1 web application backed by Azure AD Authentication and for a specific functionality I need to fetch the list of projects from Azure DevOps that the signed user is a part of.

Upvotes: 0

Views: 1322

Answers (1)

Leo Liu
Leo Liu

Reputation: 76670

Fetching a list of projects for a user in Azure DevOps

You could achieve this with PATs (Personal access tokens) and then call the Azure DevOps API User Entitlements - Get User Entitlement.

GET https://vsaex.dev.azure.com/{organization}/_apis/userentitlements/{userId}?api-version=5.1-preview.2

To get the userId by the REST API User Entitlements:

GET https://vsaex.dev.azure.com/{organization}/_apis/userentitlements?api-version=5.1-preview.2

With this REST API, we could get the userId.

The test powershell scripts:

$connectionToken="<MyPAT>"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$ProjectsBelongUrl = "https://vsaex.dev.azure.com/<MyOrganization>/_apis/userentitlements/<userId>?api-version=5.1-preview.2" 

Write-Host "URL: $ProjectsBelongUrl"

$UserEntitlements = (Invoke-RestMethod -Uri $ProjectsBelongUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

$ProjectsList = $($Pipelines.projectEntitlements.projectRef.name | ConvertTo-Json -Depth 100)


Write-Host "User XX belog to which projects = $ProjectsList"

The test result:

enter image description here

Upvotes: 1

Related Questions