msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

delta group query call for members

Open RaHehl opened this issue 6 months ago • 2 comments

I’m referring to https://github.com/microsoftgraph/msgraph-sdk-java/issues/1170.

The last suggested solution there doesn’t seem to match the current version syntactically. I’m currently trying this with the beta client.

How do I make a request like https://graph.microsoft.com/beta/groups/delta?$filter=id eq '1234' using the client?

And how does the skip token work for the subsequent requests that are made at a later time?

PS: When creating an issue, one of the options is "I have a question about usage," and then it suggests opening a discussion instead of an issue. However, it seems that "Discussions" are no longer available in this repository?

RaHehl avatar Jun 04 '25 14:06 RaHehl

that seems to be the right call var result = graphClient.groups().delta().get(requestConfiguration -> requestConfiguration.queryParameters.filter = "id eq '"+groupId+"'"); however, getMembers is null

In the rest request the result that is relevant for me is in the field members@delta

Is it possible that the deserialization for members@delta is missing?

RaHehl avatar Jun 06 '25 07:06 RaHehl

I've got the whole thing working now, but it doesn't seem completely optimal to me. Is it supposed to be like this, or is there a better way to do it?

package org.example;

 import com.azure.identity.ClientSecretCredentialBuilder;
 import com.microsoft.graph.beta.models.User;
 import com.microsoft.graph.beta.serviceclient.GraphServiceClient;
 import com.microsoft.kiota.serialization.UntypedArray;
 import com.microsoft.kiota.serialization.UntypedNode;

 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;

 public class App {
     public static void main(String[] args) {
         var credential = new ClientSecretCredentialBuilder()
                 .clientId("xxx")
                 .tenantId("xxx")
                 .clientSecret("xxx")
                 .build();

         var graphClient = new GraphServiceClient(credential, new String[]{"https://graph.microsoft.com/.default"});
         var groupId = "xxx";

         var page = graphClient.groups().delta()
                 //.withUrl(nextLink)
                 .get(config -> config.queryParameters.filter = "id eq '" + groupId + "'");

         List<String> removedMembers = new ArrayList<>();
         List<String> addedMembers = new ArrayList<>();

         page.getValue().forEach(group -> {
             var delta = (UntypedArray) group.getAdditionalData().get("members@delta");
             delta.getValue().forEach(value -> {
                 if (value.getValue() instanceof Map) {
                     var memberMap = (Map<String, UntypedNode>) value.getValue();
                     var idNode = memberMap.get("id");
                     var dataTypeNode = memberMap.get("@odata.type");

                     if (idNode != null && dataTypeNode != null) {
                         var memberId = (String) idNode.getValue();
                         var dataType = (String) dataTypeNode.getValue();

                         if ("#microsoft.graph.user".equals(dataType)) {
                             if (memberMap.containsKey("@removed")) {
                                 removedMembers.add(memberId);
                             } else {
                                 addedMembers.add(memberId);
                             }
                         }
                     }
                 }
             });
         });

         //String nextLink = page.getOdataNextLink();

         var removedUsers = removedMembers.stream()
                 .map(memberId -> fetchUser(graphClient, memberId))
                 .filter(user -> user != null)
                 .collect(Collectors.toList());

         var addedUsers = addedMembers.stream()
                 .map(memberId -> fetchUser(graphClient, memberId))
                 .collect(Collectors.toList());

         printUsers("Added User", addedUsers);
         printUsers("Removed User", removedUsers);
     }

     private static User fetchUser(GraphServiceClient graphClient, String memberId) {
         try {
             return graphClient.users().byUserId(memberId).get();
         } catch (Exception e) {
             return null;
         }
     }

     private static void printUsers(String label, List<User> users) {
         users.forEach(user -> System.out.printf("%s: %s (%s) - %s - %s%n",
                 label, user.getDisplayName(), user.getId(), user.getMail(), user.getOnPremisesSamAccountName()));
     }
 }

RaHehl avatar Jun 10 '25 09:06 RaHehl