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

Alex Williams 1 Reputation point
2022-06-12T09:55:17.447+00:00

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

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Zehui Yao_MSFT 5,876 Reputation points
    2022-06-13T06:33:17.897+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.