Get Group Users Using CSOM

The following example returns all the users from the given SharePoint Site Group using SharePoint Managed Client Side Object Model.
 
After creating a console application project in Visual Studio solution, add the following assembly references to the project.
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
  1. using Microsoft.SharePoint.Client;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Text;  
  7.   
  8. namespace GetGroupUsers  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             //Get Site Url fro user    
  15.             Console.Write("Enter Site URL: ");  
  16.             string strURL = Console.ReadLine();  
  17.   
  18.             //Get Username from user in the format of (Domain/Login ID)    
  19.             Console.Write("Enter UserName (domain/userid): ");  
  20.             string strUserName = Console.ReadLine();  
  21.   
  22.             Console.Write("Enter your password: ");  
  23.             string pass = getPassword();  
  24.             Console.WriteLine();  
  25.             RETRY_GROUP:  
  26.             Console.Write("Enter Group Name: ");  
  27.             string groupName = Console.ReadLine();  
  28.             Console.WriteLine();  
  29.   
  30.             try  
  31.             {  
  32.                 ClientContext ctx = new ClientContext(strURL);  
  33.                 ctx.Credentials = new NetworkCredential(strUserName, pass);  
  34.                 Web web = ctx.Web;  
  35.   
  36.                 Group group = web.SiteGroups.GetByName(groupName);  
  37.                   
  38.                 //Parameters to receive response from the server                      
  39.                 ctx.Load(web, w => w.Title);  
  40.                 ctx.Load(group, grp => grp.Title, grp => grp.Users, grp => grp.Owner);  
  41.                 ctx.ExecuteQuery();  
  42.   
  43.                 Console.WriteLine("Groups Name: " + group.Title);  
  44.                 Console.WriteLine("Users Count: " + group.Users.Count);  
  45.                 Console.WriteLine("Group Owner: " + group.Owner.Title);  
  46.                 Console.WriteLine("Users:");  
  47.                 foreach(User usr in group.Users)  
  48.                 {  
  49.                     Console.WriteLine(usr.Title);  
  50.                 }  
  51.             }  
  52.             catch (Exception ex)  
  53.             {  
  54.                 Console.WriteLine("Error: " + ex.Message);  
  55.                 Console.Write("Do you want to continue (y / n):");  
  56.                 ConsoleKeyInfo boolContinue = Console.ReadKey();  
  57.                 Console.WriteLine();  
  58.                 if (boolContinue.KeyChar == 'y')  
  59.                     goto RETRY_GROUP;  
  60.                 else  
  61.                     Console.Write("Enter to Exit.");  
  62.             }  
  63.   
  64.             Console.Read();              
  65.         }  
  66.   
  67.         private static string getPassword()  
  68.         {  
  69.             ConsoleKeyInfo key;  
  70.             string pass = "";  
  71.             do  
  72.             {  
  73.                 key = Console.ReadKey(true);  
  74.                 // Backspace Should Not Work    
  75.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)  
  76.                 {  
  77.                     pass += key.KeyChar;  
  78.                     Console.Write("*");  
  79.                 }  
  80.                 else  
  81.                 {  
  82.                     if (key.Key == ConsoleKey.Backspace && pass.Length > 0)  
  83.                     {  
  84.                         pass = pass.Substring(0, (pass.Length - 1));  
  85.                         Console.Write("\b \b");  
  86.                     }  
  87.                 }  
  88.             }  
  89.             // Stops Receving Keys Once Enter is Pressed    
  90.             while (key.Key != ConsoleKey.Enter);  
  91.             return pass;  
  92.         }  
  93.     }  
  94. }  
Supported Versions: 

SharePoint 2013 + SharePoint Online 
Ebook Download
View all
Learn
View all