Get Available Site Groups Using CSOM

The following example returns all the Groups available in Site using SharePoint's 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 GetSiteGroups  
  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.   
  26.             ClientContext ctx = new ClientContext(strURL);  
  27.             ctx.Credentials = new NetworkCredential(strUserName, pass);  
  28.             Web web = ctx.Web;  
  29.             //Parameters to receive response from the server    
  30.             //SiteGroups property should be passed in Load method to get the collection of groups    
  31.             ctx.Load(web, w => w.Title, w => w.SiteGroups);  
  32.             ctx.ExecuteQuery();  
  33.   
  34.             GroupCollection groups = web.SiteGroups;  
  35.               
  36.             Console.WriteLine("Groups associated to the site: " + web.Title);  
  37.             Console.WriteLine("Groups Count: " + groups.Count.ToString());  
  38.             foreach(Group grp in groups)  
  39.             {  
  40.                 Console.WriteLine(grp.Title);  
  41.             }  
  42.             Console.Read();  
  43.         }  
  44.   
  45.         private static string getPassword()  
  46.         {  
  47.             ConsoleKeyInfo key;  
  48.             string pass = "";  
  49.             do  
  50.             {  
  51.                 key = Console.ReadKey(true);  
  52.                 // Backspace Should Not Work    
  53.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)  
  54.                 {  
  55.                     pass += key.KeyChar;  
  56.                     Console.Write("*");  
  57.                 }  
  58.                 else  
  59.                 {  
  60.                     if (key.Key == ConsoleKey.Backspace && pass.Length > 0)  
  61.                     {  
  62.                         pass = pass.Substring(0, (pass.Length - 1));  
  63.                         Console.Write("\b \b");  
  64.                     }  
  65.                 }  
  66.             }  
  67.             // Stops Receving Keys Once Enter is Pressed    
  68.             while (key.Key != ConsoleKey.Enter);  
  69.             return pass;  
  70.         }  
  71.     }  
  72. }