4

I want to get a user and all his groups by a login name. Easy to do in Sharepoint 2013.

        clientContext.Load(site, l => l.RootWeb.SiteUsers, l => l.RootWeb.Id, l => l.Id);
        clientContext.ExecuteQuery();

        var user = site.RootWeb.SiteUsers.GetByLoginName(loginName);
        clientContext.Load(user, l => l.Groups, l => l.Id, l => l.LoginName);
        clientContext.ExecuteQuery();

As both the SiteUsers as well as the Groups Property don't exist in SP2010 i have no idea how to accomplish this. Any ideas who i can get the needed information?

The only information i can use is the login name, CurrentUser or something similar won't work.

EDIT: I have found a way to get the User object via CurrentUser, nevertheless i can't find a reliable way to get the groups for this user. I can iterate through all groups of the site and check if they contain the user. But then i have to also resolve all AD Groups and consider special AD Groups like "NT AUTHORITY\authenticated users". Makes the whole process rater slow. Is there a faster way to do this in SP2010?

2

You could consider the following approach to retrieve user groups using SharePoint 2010 CSOM:

Example

var user = site.RootWeb.CurrentUser;
var siteGroups = site.RootWeb.SiteGroups; 
ctx.Load(user);
ctx.Load(siteGroups, gcol => gcol.Include(g => g.LoginName,g => g.Users));
ctx.ExecuteQuery();
var userGroups = siteGroups.ToList().Where(g => g.Users.ToList().Exists(u => u.Id == user.Id)); //filter groups     
  • here variable userGroups will return all properties of that groups?? Because I tried this code 2 days back, and I was not able to iterate through that collection and get any details of groups... Finally I followed different approach – Gaurravs Dec 12 '15 at 18:17
  • Its loading all groups from site with all users... with big sharepoints with many groups and users per site this will be a problematic performance hungry query, in fact it could get throttled by the server – Michal Hainc Aug 7 '18 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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