0

How do I get all site collection users programmatically? I want all the users not the groups.

  • Via Compiled Code (C#) or Script (PowerShell)? – Benny Skogberg May 26 '15 at 11:43
  • Not with powershell. Via Complied code – DRS May 26 '15 at 12:03
2

Use SPWeb.SiteUsers to get all users in the site collection.

0

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);
            }
        }  
    }
}

}

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.