'Get list of projects user has access to

I have a site for automation, and want to display to users a list of projects they have access to in a dropdown. If I have a a PAT for an admin account in the org, how can I get the list of projects given a user's email?

Presumably the REST api is the best way to do this?

https://docs.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1



Solution 1:[1]

I've found two ways to do this.

First method: list all projects, filter based on user list

projects = GET https://dev.azure.com/{orgname}/_apis/projects?api-version=6.0
results = []
foreach project in projects:
    descriptor = GET https://vssps.dev.azure.com/{orgname}/_apis/graph/descriptors/{project.id}
    members = GET https://vssps.dev.azure.com/{orgname}/_apis/graph/users?api-version=6.0-preview.1&scopeDescriptor={descriptor}
    if userId in members:
        results.push(project)

Second method: get user entitlements
This is better because this will show all projects that people have Reader (or higher) on, where the first method doesn't show projects where the user doesn't have explicit membership

users = GET https://vsaex.dev.azure.com/{orgname}/_apis/UserEntitlements?$filter=name eq '{userId}'&$orderBy=name Ascending&select=Projects
user = [x for x in users where x.userId == userId][0]
results = user.projectEntitlements

Note the select query parameter included in the second example, this is necessary for projectEntitlements to be included in the result.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TeamDman