The azure Graph Client library was updated the 22th of december and the method add user to group was fixed.
Azure Active Directory Graph Client 2.0 - Context is not currently tracking the entity
But is it possible to remove an user from a group?
I tried this method:
{groupObject}.Members.Remove({entityObject} as DirectoryObject);
await myGroup.UpdateAsync();
It does not fail but the user is not deleted from the group.
I have found a workaround. Maybe this will help:
public void RemoveUserFromGroup(Group group, User user)
{
var internalGroup = _activeDirectoryClient.Context.CreateQuery<GraphClient.Internal.Group>("groups/" + group.ObjectId).ToList().First();
var internalUser = _activeDirectoryClient.Context.CreateQuery<GraphClient.Internal.User>("users/" + user.ObjectId).ToList().First();
_activeDirectoryClient.Context.DeleteLink(internalGroup, "members", internalUser);
_activeDirectoryClient.Context.SaveChanges();
}
I ran into a similar problem and was able to diagnose it. The problem I believe depends on how the group is retrieved -- whether the group's members are included; you can use an .Expand() clause for this.
For example, the following does work:
group = (Group)(await _activeDirectoryClient.Groups.Where(g => g.ObjectId == groupId).Expand(g => g.Members).ExecuteSingleAsync());
user = (User)(await _activeDirectoryClient.Users.Where(u => u.ObjectId == userId).ExecuteSingleAsync());
group.Members.Remove(user);
await group.UpdateAsync();
Note, however, that the .Expand() operation is limited to 20 objects, so in most cases, the solution from Tomáš is probably safer at the moment.
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