2

I need to get users from a group, i get a group name instead of group id from UI. Is there a java sdk api to get users directly from group through the group name.

I have the below code to get the users through the group id

public List<String>  getAllUsersInAzureADGroupByID(String token, String groupId) {
    log.debug("in getAllUsersInAzureADGroupByID()   :start...");
    List<String> users = new ArrayList<>();
    try {
        validateToken(token);
        @SuppressWarnings("rawtypes")
        GraphServiceClient graphClient = getGraphClient();
        UserCollectionRequestBuilder usersClientBuilder = graphClient.groups(groupId).transitiveMembersAsUser();
        UserCollectionRequest usersRequest = usersClientBuilder.buildRequest();
        do {
            UserCollectionPage userCollectionPage = usersRequest.get();
            List<User> currentPage = userCollectionPage.getCurrentPage();

            for (int i = 0; i< currentPage.size(); i++) {
                User user = currentPage.get(i);
                users.add(user.userPrincipalName.toLowerCase());
            }

            usersClientBuilder = userCollectionPage.getNextPage();
            if (usersClientBuilder == null) {
                usersRequest = null;
            } else {
                usersRequest = usersClientBuilder.buildRequest();
            }
        } while (usersRequest != null);

    } catch (Exception e) {
        log.error("Exception occurred in getAllUsersInAzureADGroupByID() : ", e);
    }
    log.debug("in getAllUsersInAzureADGroupByID()   :end...");
    return users;
}
  • To get the group by group name should i use the search or filter ?

  • Which will return the exact match is it the filter or search ?

I have tried the following in postman

https://graph.microsoft.com/v1.0/groups?$search="displayName:Team-Testing-Graph"

And it returns the data

So is it that first i should get the group and take the id from the group and then call my above method which takes the group id and returns the upn of the user.

Is there a direct way to get the users from a group based on the group name ?

Any help/suggestions

Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44

0 Answers0