Get Web Groups With Permissions Using CSOM

The following example returns all the Groups with permissions associated to the SharePoint Web using 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 GetWebGroups  
  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.             //RoleAssignments property should be passed in Load method to get the collection of Groups assigned to the web    
  31.             ctx.Load(web, w => w.Title);  
  32.             RoleAssignmentCollection roleAssignments= web.RoleAssignments;  
  33.             //RoleAssignment.Member property returns the group associated to the web  
  34.             //RoleAssignement.RoleDefinitionBindings property returns the permissions associated to the group for the web  
  35.             ctx.Load(roleAssignments, roleAssignement => roleAssignement.Include(r => r.Member, r => r.RoleDefinitionBindings));  
  36.             ctx.ExecuteQuery();              
  37.   
  38.             Console.WriteLine("Groups has permission to the Web: " + web.Title);  
  39.             Console.WriteLine("Groups Count: " + roleAssignments.Count.ToString());  
  40.             Console.WriteLine("Group with Permissions as follows:");  
  41.             foreach (RoleAssignment grp in roleAssignments)  
  42.             {  
  43.                 string strGroup = "";                  
  44.                 strGroup += grp.Member.Title +" : ";  
  45.                   
  46.                 foreach (RoleDefinition rd in grp.RoleDefinitionBindings)  
  47.                 {                  
  48.                     strGroup += rd.Name+ " ";                   
  49.                 }  
  50.                 Console.WriteLine(strGroup);  
  51.             }  
  52.             Console.Read();  
  53.         }  
  54.   
  55.         private static string getPassword()  
  56.         {  
  57.             ConsoleKeyInfo key;  
  58.             string pass = "";  
  59.             do  
  60.             {  
  61.                 key = Console.ReadKey(true);  
  62.                 // Backspace Should Not Work    
  63.                 if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)  
  64.                 {  
  65.                     pass += key.KeyChar;  
  66.                     Console.Write("*");  
  67.                 }  
  68.                 else  
  69.                 {  
  70.                     if (key.Key == ConsoleKey.Backspace && pass.Length > 0)  
  71.                     {  
  72.                         pass = pass.Substring(0, (pass.Length - 1));  
  73.                         Console.Write("\b \b");  
  74.                     }  
  75.                 }  
  76.             }  
  77.             // Stops Receving Keys Once Enter is Pressed    
  78.             while (key.Key != ConsoleKey.Enter);  
  79.             return pass;  
  80.         }  
  81.     }  
  82. }