0

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

2
  • Via Compiled Code (C#) or Script (PowerShell)? Commented May 26, 2015 at 11:43
  • Not with powershell. Via Complied code Commented May 26, 2015 at 12:03

2 Answers 2

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

}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.