OlivierF
OlivierF

Reputation: 51

Azure GraphClient library remove User from Group

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.

Upvotes: 5

Views: 1633

Answers (2)

Robin Carduner
Robin Carduner

Reputation: 31

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.

Upvotes: 3

Tomas Prokop
Tomas Prokop

Reputation: 141

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();           
    }

Upvotes: 13

Related Questions