Batch add members to groups with graph API fails

Antoine Le Livec 0 Reputation points
2023-06-21T22:58:39.2533333+00:00

Hello everyone,

I'm having an issue using the Graph API to add users to groups in my B2C tenant.

Give the volume to be processed, we have no other choice than to proceed with batch requests (https://learn.microsoft.com/en-us/graph/json-batching). Unfortunately, when we try to apply that by adding members to groups :

{
    "requests": [
        {
            "id": "1",
            "url": "https://graph.microsoft.com/v1.0/groups/{MyGroupID}/members/$ref",
            "method": "POST",
            "body": {
                "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/{MyUserID}"
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}

I get the following error :

{
    "responses": [
        {
            "id": "1",
            "status": 400,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "error": {
                    "code": "BadRequest",
                    "message": "Resource not found for the segment 'https:'.",
                    "innerError": {
                        "date": "2023-06-21T22:33:16",
                        "request-id": "{MyRequestID}",
                        "client-request-id": "{MyClientRequestID}"
                    }
                }
            }
        }
    ]
}

The effect is the same if I send multiples requests of the same type in the batch request. I get a 400 for each of them with the same error message.

I tried to use the exact same parameters in a "classic" single graph API call to see if something was wrong, but with the same IDs, on the same tenant, logged with the same account, I get a 204, and the group is added as expected :

User's image

So it's not about permissions nor context. Is the graph API simply not able to add new members to groups in a batch call ? That would be surprising...

If anyone has an idea that would be very helpful because it doesn't make any sense to me.

Thanks,

Antoine

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2023-06-22T10:04:38.1866667+00:00

    Hi @Antoine Le Livec

    You can of course add new members to the group in batches, which does not require calling the batch API, you just need to call the /groups/{group-id} endpoint and send a PATCH request.

    PATCH https://graph.microsoft.com/v1.0/groups/{group-id}
    Content-type: application/json
    
    {
      "******@odata.bind": [
        "https://graph.microsoft.com/v1.0/directoryObjects/{id}",
        "https://graph.microsoft.com/v1.0/directoryObjects/{id}",
        "https://graph.microsoft.com/v1.0/directoryObjects/{id}"
        ]
    }
    

    Note that up to 20 members can be added in a single request.

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


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.