I am trying to add multiple users async with AAD graph Like this:
IUser user1 = ...;
IUser user2 = ...;
IUser user3 = ...;
List<Task> addTasks = new List<Task>();
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user1));
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user2));
addTasks.Add(activeDirectoryClient.Users.AddUserAsync(user3));
await Task.WhenAll(addTasks);
I get this error message :
{"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"Another object with the same value for property userPrincipalName already exists."},"values":[{"item":"PropertyName","value":"userPrincipalName"},{"item":"PropertyErrorCode","value":"ObjectConflict"}]}}
If I use this code :
IUser user1 = ...;
IUser user2 = ...;
IUser user3 = ...;
List<Task> addTasks = new List<Task>();
await activeDirectoryClient.Users.AddUserAsync(user1);
await activeDirectoryClient.Users.AddUserAsync(user2);
await activeDirectoryClient.Users.AddUserAsync(user3);
This is working well.
Maybe we can't add multiple users in AAD at the same time ?
EDIT Here is the users part:
IUser newStudentUser = new User
{
DisplayName = $"Etudiant de l'école {school}",
UserPrincipalName = $"etudiant-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Etudiant {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};
IUser newTeacherUser = new User
{
DisplayName = $"Professeur de l'école {school}",
UserPrincipalName = $"professeur-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Professeur {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};
IUser newDirectorUser = new User
{
DisplayName = $"Directeur de l'école {school}",
UserPrincipalName = $"directeur-{school}@........fr",
AccountEnabled = true,
MailNickname = $"Directeur {school}",
UsageLocation = "US",
PasswordProfile = new PasswordProfile
{
Password = "......."
}
};
In your case, you want to batch the creation of multiple users.
At present, the GraphClient does support batch processing but there are some limitation (Batch processing | Graph API concepts):
So in your case you can't batch the creation of more than 5 users.
While adding an entity to Graph API client, you can choose to defer the execution of the query using the deferredSave
parameter.
await activeDirectoryClient.Users.AddUserAsync(user, deferredSave: true);
The Graph API client has a DataServiceContextWrapper
that tracks changes. It provides a SaveChanges(Async)
method.
await activeDirectoryClient.Context.SaveChangesAsync();
Calling this method you can specify the SaveChangesOptions:
Now you have enough information to write the code to batch the creation of the users:
// Only 5 users per batch !!!!
var user1 = ...;
var user2 = ...;
var user3 = ...;
await activeDirectoryClient.Users.AddUserAsync(newStudentUser, deferredSave: true);
await activeDirectoryClient.Users.AddUserAsync(newTeacherUser, deferredSave: true);
await activeDirectoryClient.Users.AddUserAsync(newDirectorUser, deferredSave: true);
// In debug mode, you should use the SaveChangesAsync method with the default options
// Becasue the BatchWithIndependentOperations will not throw any exception even if there is a problem while creating the user.
//await activeDirectoryClient.Context.SaveChangesAsync();
await activeDirectoryClient.Context
.SaveChangesAsync(SaveChangesOptions.BatchWithIndependentOperations);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With