6

I have an SPGroup object populated, how can I remove all members from the group, so that essentially I start with a fresh group.

I can't delete and recreate it, so I need a way to work with the existing SharePoint group.

Thanks in advance

11

Take a look on msdn

    foreach (SPUser user in group.Users)
    {
        group.RemoveUser(user);
    }

But be aware that you change this group for the whole site collection, if you have other site collections those won't be impacted by the modification of this group.

  • 2
    Won't this throw a modify collection exception? – JL01 Sep 16 '11 at 11:55
  • 1
    I have tested it and it works. – Anatoly Mironov Sep 16 '11 at 12:18
  • 1
    Neat, was looking for this. I was afraid that this would in fact skip every other user due to the indexes changing as users get removed, as is the situation when removing groups for example. Tried it, and it worked fine. Nice solution Anatoly. =) – Johannes Jan 31 '12 at 16:12
  • I'm not sure how this works since the collection is being modified directly. Is it because this is server code and it gets a new instance of the collection during each iteration? I know the CSOM version of this would not work. – Dinerdo Apr 13 '16 at 20:05
  • @JL01 I believe this works because the foreach() call to the Users property creates a new SPUserCollection object and gets an enumerator to that collection. The RemoveUser call itself just gets the ID from the user object before calling its Users property (generating a new SPUserCollection) to delete the user by ID. It looks like each new SPUserCollection gets a copy of the data so you're not enumerating on the same collection you're modifying (and also creating n+1 SPUserCollection objects where n = number of users in group) – Erik Noren Apr 18 '18 at 17:07
0
   while (group.Users.Count > 0)
     {
        group.Users.Remove(group.Users[0]);
     }

This should remove all the users in the group for CSOM. Edit: It would be inefficient for SSOM, however.

  • I believe every call to .Users returns a new SPUserCollection so in this case you will create 3n+1 new collections (.Count(), .Remove(), [0]) where n is the number of users in the group (the +1 is the final check where .Count == 0). – Erik Noren Apr 18 '18 at 16:37
  • Maybe with SSOM, but you believe that is the case in CSOM once the users are already loaded? – Dinerdo Apr 18 '18 at 20:06
  • Ah, I was reading the question as specific to SSOM and my comment was related to that. I haven't used the CSOM but I do know they share some common implementation. I would expect CSOM to implement some caching or proxying to prevent excessive network chatter so I don't know the impact there. – Erik Noren Apr 18 '18 at 22:01
  • Ah okay. You did confirm the answer to a question in my comment on the accepted answer though. That's an important clarification. – Dinerdo Apr 19 '18 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.