I want to get all group that current user is member in SharePoint server 2010 using Client Object Model C#. Anyone help me.
-
There is another post asking same question with useful answers.– Mark LCommented 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?– RamCommented Oct 18, 2016 at 5:20
-
Please check my Updated answer– Akshay Dattatray NangareCommented Oct 18, 2016 at 7:03
Add a comment
|
3 Answers
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
}
}
}
-
-
If my answer help you to find solution then, mark this answer as accepted. Commented Oct 18, 2016 at 9:55
-
-
Find tick (Check mark) , at the left corner of my answer. If you not found then ignore Commented 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....– michalhCommented Aug 7, 2018 at 9:03
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.
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;
}
}
}
...
-
You need to use ClientContext insted of SPSite Commented Oct 18, 2016 at 8:44
-