Cannot see displayName of Group returned by Graph API users/{id}/memberOf call (Java)

1.4k Views Asked by At

I have successfully followed this MS Graph Java tutorial all the way through: https://learn.microsoft.com/en-gb/graph/tutorials/java?tabs=aad

I have added my own Graph API call that returns the Groups that a given User is a member of using this code:

In App.java file

private static void makeGraphCall() {
        try {
            final DirectoryObjectCollectionWithReferencesPage memberof = Graph.makeGraphCall();
            for (DirectoryObject group : memberof.getCurrentPage()) {
                System.out.println(group.id);
            }
        } catch (Exception e) {
            System.out.println("Error making Graph call");
            System.out.println(e.getMessage());
        }
    }

In Graph.java file

public static DirectoryObjectCollectionWithReferencesPage makeGraphCall() throws Exception {
    
        ensureGraphForAppOnlyAuth();
        return _appClient.users("{here I enter the User ID}").memberOf()
                .buildRequest()
                .get();
    }

I used this reference to help writing the above code: https://learn.microsoft.com/en-us/graph/api/user-list-memberof?view=graph-rest-1.0&tabs=http in particular this part:

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

DirectoryObjectCollectionWithReferencesPage memberOf = graphClient.users("{id}").memberOf()
    .buildRequest()
    .get();

When I run my code I get the id's of the Groups the given User is a member of which is great. However, I want to get the displayName of each Group. If I add:

System.out.println(group.displayName);

to my code in App.java, I get an error stating:

error: cannot find symbol
                System.out.println(group.displayName);
                                        ^
  symbol:   variable displayName
  location: variable group of type DirectoryObject

I understand that my call to the Graph API is made using the _appClient (as opposed to the _userClient) and I have therefore made sure that my application registered in Azure has Directory.Read.All and Directory.ReadWrite.All application permissions.

I have also tested my call to the Graph API in the Graph Explorer using the same User id i.e.

GET https://graph.microsoft.com/v1.0/users/{here i enter the User's id}/memberOf 

and it returns full details of that User's Group memberships including the displayName of each Group.

I have been searching for an answer for several days now and cannot understand where I am going wrong. Please could anyone help with my understanding and tell me how (and ideally explain why) I am not able to show the displayName of each Group the User is a member of?

Thanks for your help

1

There are 1 best solutions below

0
On BEST ANSWER

You are casting the results to directoryObject but directoryObject has no property displayName.

for (DirectoryObject group : memberof.getCurrentPage()) {
    System.out.println(group.id);
}

Try to cast the results to group and you should be able to read displayName.

for (DirectoryObject directoryObject : memberof.getCurrentPage()) {
    if ( directoryObject instanceof Group) {
        Group group = (Group)directoryObject;
        System.out.println(group.id);
        System.out.println(group.displayName);
    }            
}