I want to get all group that current user is member in SharePoint server 2010 using Client Object Model C#. Anyone help me.
Asked
Active
Viewed 4,341 times
0
-
There is another post asking same question with useful answers. – Mark L Oct 18 '16 at 01: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 '16 at 05:20
-
Please check my Updated answer – Akshay Dattatray Nangare Oct 18 '16 at 07:03
3 Answers
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
}
}
}
Akshay Dattatray Nangare
- 3,392
- 9
- 59
- 105
-
-
If my answer help you to find solution then, mark this answer as accepted. – Akshay Dattatray Nangare Oct 18 '16 at 09:55
-
-
Find tick (Check mark) , at the left corner of my answer. If you not found then ignore – Akshay Dattatray Nangare Oct 18 '16 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 07 '18 at 09: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.
Tomás Vélez
- 11
- 1
-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