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 L– Mark L2016-10-18 01:18:16 +00:00Commented 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– Ram2016-10-18 05:20:48 +00:00Commented Oct 18, 2016 at 5:20
-
Please check my Updated answerAkshay Dattatray Nangare– Akshay Dattatray Nangare2016-10-18 07:03:19 +00:00Commented 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
}
}
}
-
good to hear from you!Akshay Dattatray Nangare– Akshay Dattatray Nangare2016-10-18 09:38:33 +00:00Commented Oct 18, 2016 at 9:38
-
If my answer help you to find solution then, mark this answer as accepted.Akshay Dattatray Nangare– Akshay Dattatray Nangare2016-10-18 09:55:56 +00:00Commented Oct 18, 2016 at 9:55
-
i don't see any link or button to mark you answer.thien thai– thien thai2016-10-18 10:04:18 +00:00Commented Oct 18, 2016 at 10:04
-
Find tick (Check mark) , at the left corner of my answer. If you not found then ignoreAkshay Dattatray Nangare– Akshay Dattatray Nangare2016-10-18 10:12:32 +00:00Commented 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– michalh2018-08-07 09:03:02 +00:00Commented 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 SPSiteAkshay Dattatray Nangare– Akshay Dattatray Nangare2016-10-18 08:44:23 +00:00Commented Oct 18, 2016 at 8:44
-
Sorry, I misread the question. Downvote accepted.Andy Wijaya– Andy Wijaya2016-10-18 09:31:33 +00:00Commented Oct 18, 2016 at 9:31