Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem while adding a User to multiple Groups in V5.63.0 #2755

Closed
Angl0r opened this issue Nov 21, 2024 · 1 comment
Closed

Problem while adding a User to multiple Groups in V5.63.0 #2755

Angl0r opened this issue Nov 21, 2024 · 1 comment
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned

Comments

@Angl0r
Copy link

Angl0r commented Nov 21, 2024

Hi,
I have a little tool which creates and manages User for me (local Active Directory -> Sync to Azure AD). I can assigne a User to multiple Groups and this worked just fine with Graph api SDK < V5. I Used to do the following:

var directoryObject = new DirectoryObject
        {
            Id = userid
        };
        try
        {
            foreach (String i in groupids)
            {
                await graphClient.Groups[i].Members.References
                .Request()
                .AddAsync(directoryObject);
            }
        }

Now with the new Update to Graph API SDK V5 a lot has changed and i got everythink working (Get Users, Get Groups, Get&Set Licenses, etc...) except this step. To do it for SDK v5 i do the following:

var requestbody = new ReferenceCreate
 {
     OdataId = graphClient.DirectoryObjects[userid].ToGetRequestInformation().URI.ToString(),
 };
foreach (String i in groupids)
{
await graphClient.Groups[i].Members.Ref.PostAsync(requestbody);
}

This always works for ONE! Group. When the Second Group is set, I get the following Error:

an unexpected 'end of input' node was found when reading from the json reader

So i guessed i try doing it in Bulk:

var requestbody = new ReferenceCreate
{
    OdataId = graphClient.DirectoryObjects[userid].ToGetRequestInformation().URI.ToString(),
};

try
{
    var batchRequestContent = new BatchRequestContentCollection(graphClient);

    foreach (String i in groupids)
    {
        var requestStepId = await batchRequestContent.AddBatchRequestStepAsync(graphClient.Groups[i].Members.Ref.ToPostRequestInformation(requestbody));
}

var batchResponse = await graphClient.Batch.PostAsync(batchRequestContent);

And again, even with the Bulk Method, the User is only added to ONE Group.

If i look inside the Responsemessage from the failing step of the Bulk i got:

Bad Request

yet if i try the same Update with Graph Explorer everything works fine.

What is my error here? The Group-IDs are correct and the User-ID is correct. Is there an easy way to add ONE user to Multiple Groups in Graph API SDK v5? Or am i just missing a hughe Point?

@Angl0r Angl0r added the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Nov 21, 2024
@Angl0r
Copy link
Author

Angl0r commented Nov 22, 2024

The issue is with ReferenceCreate, you need to create a new instance of ReferenceCreate for each group. The ReferenceCreate and other Graph SDK models implement the backing store that tracks changes in properties. Once the instance of ReferenceCreate is serialized to the body of the first request, it's not serialized in other requests.

foreach (var groupId in groupIds)
{
    var referenceCreate = new ReferenceCreate
    {
        OdataId = $"https://graph.microsoft.com/v1.0/directoryObjects/${userId}"
    };
    var request = graphClient.Groups[groupId].Members.Ref.ToPostRequestInformation(referenceCreate);
    await batchRequestContent.AddBatchRequestStepAsync(request);
}

var returnedResponse = await graphClient.Batch.PostAsync(batchRequestContent);

Credits to user2250152

@Angl0r Angl0r closed this as completed Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned
Projects
None yet
Development

No branches or pull requests

1 participant