Hi,
As you're saying you can use the GroupCollectionCriterion. Here's an example of how you can get all users belonging to group 1 or 4:
List groupIds = new List{ 1, 4 };
UserQuery userQuery = new UserQuery();
userQuery.Groups = new GroupCollectionCriterion();
userQuery.Groups.Containing = new GroupCriterion();
userQuery.Groups.Containing.ID = new IntegerCriterion();
userQuery.Groups.Containing.ID.Includes = new IntegerInCriterion();
userQuery.Groups.Containing.ID.Includes.Values.AddRange(groupIds);
Karoline
Thank you Karoline, that did the trick!
If one would like to exclude users of a certain group (8), how do I do that?
It seems that this will not work:
membersQuery.Groups.Containing.ID.Operator = ComparisonOperator.NotEquals;
membersQuery.Groups.Containing.ID.Value = 8;
I still get users belonging to 8, if they at the same time belongs to som other group.
Jonas
That was a tricky one, I've tried to find a solution but I ran into the same problems as you: You get the user if they belong to some other group as well.
The only solution I can think of right now should work, but it's not a very good solution:
UserCollection allUsers = CommunitySystem.CurrentContext.DefaultSecurity.GetUsers();
UserQuery excludedUserQuery = new UserQuery();
excludedUserQuery.Groups = new GroupCollectionCriterion();
excludedUserQuery.Groups.Containing = new GroupCriterion();
excludedUserQuery.Groups.Containing.ID = new IntegerCriterion();
excludedUserQuery.Groups.Containing.ID.Value = 593;
UserCollection excludedUsers = QueryHandler.GetQueryResult(excludedUserQuery);
// Get all users who are not in the excludedUsers UserCollection
IEnumerable users = from u in allUsers where !excludedUsers.Contains(u) select u;
I'll think a little bit about it to see if I can come up with a better one :)
Karoline
I can't really get my UserQuery working as I want. What I want is to select all users belonging to group 1 or 4.
I should use the GroupsCollectionCriterion but how do I get it to query for group 1 or 4?