Add/remove users to/from AAD group in batches

What is the code to add users to AAD group or remove users from AAD group in batches in C#? (first find batch size and then add or remove users). Any sample code would be great.

UPDATE:

I added the following code:

        private HttpRequestMessage MakeRequest(AzureADUser user, Guid targetGroup)
        {
            return new HttpRequestMessage(HttpMethod.Patch, $"https://graph.microsoft.com/v1.0/groups/{targetGroup}")
            {
                Content = new StringContent(MakeAddRequestBody(user), System.Text.Encoding.UTF8, "application/json"),
            };
        }

        private static string MakeAddRequestBody(AzureADUser user)
        {
            JObject body = new JObject
            {
                ["members@odata.bind"] = JArray.FromObject($"https://graph.microsoft.com/v1.0/users/{user.ObjectId}")
            };
            return body.ToString(Newtonsoft.Json.Formatting.None);
        }

        public async Task AddUsersToGroup1(IEnumerable<AzureADUser> users, AzureADGroup targetGroup)
        {           
            try
            {                
                var batches = GetBatchRequest(users, targetGroup.ObjectId);
                foreach (var batchRequestContent in batches)
                {
                    var response = await _graphServiceClient
                        .Batch
                        .Request()
                        .WithMaxRetry(10)
                        .PostAsync(batchRequestContent);
                    var responses = await response.GetResponsesAsync();
                }
            }
            catch (Exception ex)
            {
            }
        }

On running this I get the following exception: Object serialized to String. JArray instance expected. What am I missing? Also, once I get the responses, I need to check if all of the response returned an 'OK' response or not similar to:

return responses.Any(x => x == ResponseCode.Error) ? ResponseCode.Error : ResponseCode.Ok;

How would I do that?



from Recent Questions - Stack Overflow https://ift.tt/3do28Or
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation