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?
2 Answers
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– devmiles.com2015-04-06 22:31:59 +00:00Commented Apr 6, 2015 at 22:31
- 1Using "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– Scubacode2015-07-17 12:51:00 +00:00Commented Jul 17, 2015 at 12:51
- Do you have any idea why the script might throw an unknown error. I used it without issues on other sites, but on one site I get an unknown error using the CSOM exampleIdra– Idra2021-03-22 16:07:07 +00:00Commented Mar 22, 2021 at 16:07
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 = "[email protected]"; //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
- 1The 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– devmiles.com2015-04-06 16:57:35 +00:00Commented Apr 6, 2015 at 16:57
- did you check the 2nd link on my post.2015-04-06 17:01:45 +00:00Commented Apr 6, 2015 at 17:01