Page 1 of 1

How do you remove a user from a group programmatically?

Posted: Wed Jun 29, 2011 11:41 am
by damionhickman
Im able to add a user to a group, but seems that removing a user from a group at runtime looks like it works but is not saved to the database.

Here's what I am using to test with:

Code: Select all

Token.Instance.User.UserGroups.RemoveAt(Token.Instance.User.UserGroups.IndexOf(Token.Instance.User.UserId, 9));
Token.Instance.User.UserGroups.Save();
Token.Instance.User.Save();
I have code right afterwards that enumerates all of the users' groups, and sure enough 9 is removed - but If i comment it out and load it up 9 is back. How can I save it after I remove it?

Re: How do you remove a user from a group programmatically?

Posted: Thu Jun 30, 2011 9:17 am
by s_ismail
Hello,
Try this code for removing user from a group

Code: Select all

User user = UserDataSource.Load(userId);
            int index = user.UserGroups.IndexOf(userId, _GroupId);
            if (index > -1) user.UserGroups.DeleteAt(index);

Re: How do you remove a user from a group programmatically?

Posted: Thu Jun 30, 2011 6:00 pm
by damionhickman
thanks that worked great!