0

I want to get all group that current user is member in SharePoint server 2010 using Client Object Model C#. Anyone help me.

3
  • There is another post asking same question with useful answers.
    – Mark L
    Oct 18, 2016 at 1:18
  • 1
    @Mark, The above post is using JSOM and thien thai is asking with Client Object model with C#. So is it useful?
    – Ram
    Oct 18, 2016 at 5:20
  • Please check my Updated answer Oct 18, 2016 at 7:03

3 Answers 3

2
using( ClientContext clientContext = new 
ClientContext("http://MyServer/sites/MySiteCollection"));
{

  #Get Group Collection           
  GroupCollection collGroup = clientContext.Web.SiteGroups;

  clientContext.Load(collGroup);

  clientContext.Load(collGroup,
  groups => groups.Include(
  group => group.Users));

  clientContext.ExecuteQuery();

  #iterate through group            
  foreach (Group oGroup in collGroup)
   {
     UserCollection collUser = oGroup.Users;

     foreach (User oUser in collUser)
       {
         #check if user is exist or not
        }
     }
}

MSDN

5
  • good to hear from you! Oct 18, 2016 at 9:38
  • If my answer help you to find solution then, mark this answer as accepted. Oct 18, 2016 at 9:55
  • i don't see any link or button to mark you answer.
    – thien thai
    Oct 18, 2016 at 10:04
  • Find tick (Check mark) , at the left corner of my answer. If you not found then ignore Oct 18, 2016 at 10:12
  • This is wrong... if there are thousands of groups with thousands of users, either of these 2 bad things will happen: 1. the query will throw "Using too many resources" exception on SP server or 2. it will run very long and the result will occupy a brutal amount of operating memory on the client app using csom....
    – michalh
    Aug 7, 2018 at 9:03
1

I am doing it in SharePoint 2016, maybe someone wanna know you can load only groups from current user.

using(ClientContext ctx = new ClientContext("http://MyServer/sites/MySiteCollection"))
{
    var userGroups = ctx.Web.CurrentUser.Groups;
    ctx.Load(userGroups);
    ctx.ExecuteQuery();

    return userGroups.Select(a => a.Title).ToList();
}

Hope this helps.

-3

If you need to get the Groups of a specific user, you could do something like:

...
using(SPSite site = new SPSite("http://yoursite"))
{
    using(SPWeb web = site.openWeb())
    {
        SPUser user = web.CurrentUser;
        SPGroupCollection groups = user.Groups;
        foreach(SPGroup group in groups)
        {
            // Get the necessary info, for example:
            // string groupName = group.Name;
        }
    }
}
...
2
  • You need to use ClientContext insted of SPSite Oct 18, 2016 at 8:44
  • Sorry, I misread the question. Downvote accepted. Oct 18, 2016 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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