How To Get The List Of Users With Specific Roles In SharePoint Online Using CSOM

In this blog, we will see the steps to get the list of the users with specific roles in SharePoint Online, using CSOM.

  1. using (var context = new ClientContext(siteurl))  
  2. {  
  3.    context.Credentials = new SharePointOnlineCredentials(userEmailAddress, password);  
  4.    context.Load(context.Web, w => w.Title);  
  5.    context.ExecuteQuery();  
  6.    var users = context.LoadQuery(context.Web.SiteUsers);  
  7.   
  8.    context.ExecuteQuery();  
  9.   
  10. foreach(var user in users)  
  11. {  
  12.    Console.WriteLine(user.Name);  
  13.   
  14.    Console.WriteLine(user.Email);  
  15.   
  16. }