Nyle Alliss
Nyle Alliss

Reputation: 47

How to get the teams that a user belongs to using Azure Devops REST API?

I am trying to find how to retrieve a list of teams that a user belongs to from a work item created in Azure DevOps. From the documentation I can't figure out if there is a way to retrieve the teams a user belongs to from the users userId, descriptor or displayName.

Just to clarify I am looking to obtain the teams from the Azure DevOps organisation/project that are located in the project setting menu.

Upvotes: 0

Views: 1422

Answers (2)

Chris O
Chris O

Reputation: 1

You can use the User Entitlements endpoint, too: Azure DevOps Rest API User Entitlements - List

That allows you to filter by email address:

https://vsaex.dev.azure.com/liox-glt-technicalservices/_apis/userentitlements?$filter=name%20eq%20'{user_email}'&api-version=7.1-preview.3

With that, you don't have to get the complete list of users first to find the descriptor. The memberships url is available directly in the response you get from above request, the json path should be:

.items[0].user._links.memberships.href

Sending a request to that memberships url lists all teams the user is a member of. That list contains descriptors, but links as well, that you can use to get the group names directly. Use the container url:

.value[0]._links.container.href

Sending a request to that url gives you data about the team, where I'm using "principalName" (it includes the project the team belongs to).

I'm using Python (with requests) for this and here are the 2 functions I'm using for that:

def get_user_details(session, user_email):
    url = f'https://vsaex.dev.azure.com/org-name/_apis/userentitlements?$filter=name%20eq%20\'{user_email}\'&api-version=7.1-preview.3'
    r = session.get(url)
    return r.json()

def list_groups(session, user_details):
    # user_details parameter is just returned json from get_user_details
    memberships_url = user_details['items'][0]['user']['_links']['memberships']['href']
    r = session.get(memberships_url)
    group_list = r.json()
    for group in group_list['value']:
        container_url = group['_links']['container']['href']
        r = session.get(container_url)
        group_details = r.json()
        print(group_details['principalName'])

Upvotes: 0

Bowman Zhu
Bowman Zhu

Reputation: 7146

Yes, you can.

But the REST API will not tell you directly that the user belongs to, it will tell you the descriptor of the group.

I will provide the detailed steps to get the information you want here.

1, List Users to capture all of the descriptor of the users.

https://learn.microsoft.com/en-us/rest/api/azure/devops/graph/users/list?view=azure-devops-rest-6.0&tabs=HTTP

2, Use the user's descriptor to get the User information content.

https://learn.microsoft.com/en-us/rest/api/azure/devops/graph/users/get?view=azure-devops-rest-6.0&tabs=HTTP

The response like below:

enter image description here

Sending a request to the box-selected data can get what group the user belongs to(This can only get the descriptor.).

3, Use the below API and compare to the descriptor you get from the above, you can get the name of all the groups that the user belongs to.

https://learn.microsoft.com/en-us/rest/api/azure/devops/graph/groups/list?view=azure-devops-rest-6.0&tabs=HTTP

Upvotes: 1

Related Questions