Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure GraphClient library remove User from Group

Tags:

c#

azure

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.

like image 580
OlivierF Avatar asked Dec 30 '14 18:12

OlivierF


2 Answers

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();           
    }
like image 144
Tomas Prokop Avatar answered Nov 03 '22 00:11

Tomas Prokop


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.

like image 36
Robin Carduner Avatar answered Nov 03 '22 01:11

Robin Carduner