1
votes

I am using graph API JAVA sdk(v1.6.0) and I am trying to get all the groups that a specific user is in. https://docs.microsoft.com/en-us/graph/api/user-getmembergroups?view=graph-rest-1.0&tabs=java#example According to API doc, I could do something like below:

graphClient.me()
    .getMemberGroups(securityEnabledOnly)
    .buildRequest()
    .post();

However, in java sdk 1.6.0, getMemberGroups() is not present under UserRequestBuilder.java class. https://github.com/microsoftgraph/msgraph-sdk-java/blob/0041e58287f02036c37a8ae0a1bf30f1f616991a/src/main/java/com/microsoft/graph/requests/extensions/UserRequestBuilder.java.

Am i missing something?

1

1 Answers

0
votes

I think they have replaced the getMemberOf() method with another one memberOf(). I was able to fetch all the groups for a particular user using the below code:

 public static void getUserGroups(String accessToken) {
        ensureGraphClient(accessToken);
        String upn="[email protected]";
        
         IDirectoryObjectCollectionWithReferencesPage iDirectoryObjectCollectionWithReferencesPage = graphClient.users(upn).memberOf().buildRequest().get();
         List<DirectoryObject> directoryObjects = iDirectoryObjectCollectionWithReferencesPage.getCurrentPage();
         for (DirectoryObject directoryObject : directoryObjects) {
            if(directoryObject.oDataType.equalsIgnoreCase("#microsoft.graph.group")) {
                System.out.println(directoryObject.getRawObject().get("displayName"));
            }
        }
    }