1

I'm using CSOM .NET library to access Sharepoint data via ClientContext. Is there a way to get the list of all users (including users who don't belong to any group) from Sharepoint? If not, is there a REST API that could be used for this purpose?

4

You could utilize Web.SiteUsers property to gets the all users that belong to the site collection. But since it returns all the users including system accounts, the following examples demonstrate how to return only real users.

CSOM

using (var ctx = GetContext(webUri,userName,password))
{
     var users = ctx.LoadQuery(ctx.Web.SiteUsers.Where(u => u.PrincipalType == PrincipalType.User && u.UserId.NameIdIssuer == "urn:federation:microsoftonline"));
     ctx.ExecuteQuery();
}

where

public static ClientContext GetContext(Uri webUri, string userName, string password)
{
    var securePassword = new SecureString();
    foreach (var ch in password) securePassword.AppendChar(ch);
    return new ClientContext(webUri) {Credentials = new SharePointOnlineCredentials(userName, securePassword)};
}

REST

Endpoint: /_api/web/siteusers?$filter=PrincipalType eq 1 and UserId/NameIdIssuer eq 'urn:federation:microsoftonline'
Method: GET
  • I'm not sure why, but it doesn't return one of my users. I've tried loading all of them including system accounts (without .Where) but to no avail. – devmiles.com Apr 6 '15 at 22:31
  • 1
    Using "ctx.Web.SiteUsers" will only return users that are site members. If you have not explicitly added your users to your site then they will not show here. – Scubacode Jul 17 '15 at 12:51
0

In the client object model, the list groups in a site-collection needs to be fetched first and then based on the groups another call needs to be made to SharePoint Online to get the list of users. The following code snippet explains it all.

namespace GetUsersInGroupCSOM
{

    class Program
    {
        static void Main(string[] args)
        {
            //Replace it with the url of your tenant or your site-collection
            string SiteUrl = "https://yoursite.sharepoint.com";

            System.Uri oUri = new System.Uri(SiteUrl);

            using (ClientContext oClientContext = new ClientContext(SiteUrl))
            {
                //Replace it with your user id for SharePoint Online
                string UserName = "lavsunswe@yoursite.onmicrosoft.com";


                //Replace it with your password
                string Password = "yourpassword";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                //Load the site-collection groups using CSOM
                oClientContext.Load(oClientContext.Web.SiteGroups);
                oClientContext.ExecuteQuery();

                GroupCollection oSiteCollectionGroups= oClientContext.Web.SiteGroups;
                Console.WriteLine("List of groups in the site collection");
                Console.WriteLine("-------------------------------------");

                foreach (Group  oGroup in oSiteCollectionGroups)

                {

                    Console.WriteLine(oGroup.Title);
                    Console.WriteLine("\n");



                }

                //Load the users collection in the Group 1

                oClientContext.Load(oSiteCollectionGroups[1].Users);
                oClientContext.ExecuteQuery();

                Console.WriteLine("List of users in the first group of site-collection");
                Console.WriteLine("-------------------------------------");
              foreach(User oUser in oSiteCollectionGroups[1].Users)
                {
                    Console.WriteLine(oUser.Title);
                    Console.WriteLine("\n");

                }



                Console.ReadLine();



            }



        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

Source: How to get Users and Groups in SharePoint 2013 Online using CSOM

Also check this one:

Office 365 - Retrieve User Profile Properties using CSOM with PowerShell

  • 1
    The problem is that in my organization this only returns 2 out of 10 users, 8 users just don't belong to any group. – devmiles.com Apr 6 '15 at 16:57
  • did you check the 2nd link on my post. – Waqas Sarwar MVP Apr 6 '15 at 17:01

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.