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

In this blog, we will see the list of steps to get the list of users with the specific roles in SharePoint Online, using CSOM. A SharePoint group is a collection of users, who all have the same set of permissions for the sites and its content. In SharePoint Online, there is always a specific task for the list of users, who belong to the certain roles and titles. Create a clientcontext object and then access SharePoint Online, using your Office 365 E-mail address and password, retrieve the set of site users, using Web object. The code snippet is given below to achieve the same. 

  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. }