microsoft / azure-devops-extension-sample

Sample web extension for Azure DevOps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use GraphClient to retrieve groups for the logged in user?

shajusukumaran opened this issue · comments

I am creating a custom extension and for one of my requirements, i need to check if the user is part of a specific group or not and then enable or disable some content for the user.

For this i was trying to use the GraphClient. In that i do see some functions that i can use like checkMembershipExistence, but it requires subject descriptor and container descriptor. When i checked on that i could see that getDescriptor can be used to retrieve the descriptor, but then it requires storageKey and to retrieve storageKey we can use getStorageKey, but that requires subjectDescriptor.

So, can you please provide an example of how the GraphClient can be used in a custom extension?

I am trying to achieve something like this (retrieve all the groups for the currently logged in user):
https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/memberships/list?view=azure-devops-rest-5.0#all_groups_for_a_use

I found the solution for this.
This is what i did:
Retrieved the user id: SDK.getUser().id
Used the id to get the user descriptor:

const client = getClient(GraphRestClient);
const userDescriptor = await client.getDescriptor(userId);

Finally fetched all the memberships:
const userMemberships = await client.listMemberships(userDescriptor.value);

Now, i have to filter the results.