How do I get all site collection users programmatically? I want all the users not the groups.
2 Answers
You need to itterate through all the groups in a site collection and get all users in these groups like so:
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveAllUsersAllGroups
{
static void Main()
{
ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
GroupCollection collGroup = clientContext.Web.SiteGroups;
clientContext.Load(collGroup);
clientContext.Load(collGroup,
groups => groups.Include(
group => group.Users));
clientContext.ExecuteQuery();
foreach (Group oGroup in collGroup)
{
UserCollection collUser = oGroup.Users;
foreach (User oUser in collUser)
{
Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}",
oGroup.Id, oGroup.Title, oUser.Title, oUser.LoginName);
}
}
}
}
}