How to create users and groups in bulk in Azure AD/Azure via Graph API as not able to find any way?

Mytoast Admin 285 Reputation points
2023-12-16T11:47:32.87+00:00

How to create users and groups in bulk in Azure AD/Azure via Graph API as not able to find any way?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,489 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sourabh Gupta 800 Reputation points Microsoft External Staff
    2023-12-17T12:40:49.82+00:00

    Hi Mytoast Admin,

    Thanks for reaching out.

    You can use Json batching mentioned at the link https://learn.microsoft.com/en-us/graph/json-batching which is the concept of combining multiple Graph request into one and make a batch call. One such example is shown below to add multiple users.

    POST https://graph.microsoft.com/v1.0/$batch
    
    {
        "requests": [{
                "id": "1",
                "method": "POST",
                "url": "/users",
                "body": {
                    "accountEnabled": true,
                    "displayName": "allentest01",
                    "mailNickname": "allentest01",
                    "userPrincipalName": "allentest01@{tenant}.onmicrosoft.com",
                    "passwordProfile": {
                        "forceChangePasswordNextSignIn": true,
                        "password": "{password-value}"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            }, {
                "id": "2",
                "method": "POST",
                "url": "/users",
                "body": {
                    "accountEnabled": true,
                    "displayName": "allentest02",
                    "mailNickname": "allentest02",
                    "userPrincipalName": "allentest02@{tenant}.onmicrosoft.com",
                    "passwordProfile": {
                        "forceChangePasswordNextSignIn": true,
                        "password": "{password-value}"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            }
        ]
    }
    

    Based upon this link : https://learn.microsoft.com/en-us/graph/json-batching#batch-size-limitations JSON batch requests are currently limited to 20 individual requests.

    In similar way you can you use this for creating groups. https://learn.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0&tabs=http

    and for creating multiple groups at a time, you can also use JSON batching of Graph as mentioned above.

    To Add and remove multiple users to a group in once.

    There is not an endpoint which we can use to remove users from AAD Group in batch. But there is a batch endpoint which combines multiple requests in one HTTP call. It also have a limitation of 20. So we can't delete too many users in one call.

    Working C# example is below. Please comment if following is required in PowerShell.

    GraphServiceClient graphClient = new GraphServiceClient(authProvider);
    
    var additionalData = new Dictionary<string, object>()
        {
            {"******@odata.bind", new List<string>()}
        };
    (additionalData["******@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/{id}""); // first user id
    (additionalData["******@odata.bind"] as List<string>).Add("https://graph.microsoft.com/v1.0/users/{id}""); // second user id
    
    var group = new Group
    {
        AdditionalData = additionalData
    };
    
    await graphClient.Groups["{group-id}"]
       .Request()
       .UpdateAsync(group);  
    

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

    1 person found this answer helpful.

  2. Gunther Lochstampfer 0 Reputation points
    2024-03-18T12:25:36.3133333+00:00

    I commented your answer and asked for further clarification with batch update in powershell. So please give me an answer, also for your source code offering: "Working C# example is below. Please comment if following is required in PowerShell"
    Yes it is required ;-)

    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.