0

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

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
thien thai
  • 1
  • 1
  • 2

3 Answers3

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

Akshay Dattatray Nangare
  • 3,392
  • 9
  • 59
  • 105
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;
        }
    }
}
...
Andy Wijaya
  • 755
  • 3
  • 12