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. namespace GetWebGroups
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //Get Site Url fro user
  14. Console.Write("Enter Site URL: ");
  15. string strURL = Console.ReadLine();
  16. //Get Username from user in the format of (Domain/Login ID)
  17. Console.Write("Enter UserName (domain/userid): ");
  18. string strUserName = Console.ReadLine();
  19. Console.Write("Enter your password: ");
  20. string pass = getPassword();
  21. Console.WriteLine();
  22. ClientContext ctx = new ClientContext(strURL);
  23. ctx.Credentials = new NetworkCredential(strUserName, pass);
  24. Web web = ctx.Web;
  25. //Parameters to receive response from the server
  26. //RoleAssignments property should be passed in Load method to get the collection of Groups assigned to the web
  27. ctx.Load(web, w => w.Title);
  28. RoleAssignmentCollection roleAssignments= web.RoleAssignments;
  29. //RoleAssignment.Member property returns the group associated to the web
  30. //RoleAssignement.RoleDefinitionBindings property returns the permissions associated to the group for the web
  31. ctx.Load(roleAssignments, roleAssignement => roleAssignement.Include(r => r.Member, r => r.RoleDefinitionBindings));
  32. ctx.ExecuteQuery();
  33. Console.WriteLine("Groups has permission to the Web: " + web.Title);
  34. Console.WriteLine("Groups Count: " + roleAssignments.Count.ToString());
  35. Console.WriteLine("Group with Permissions as follows:");
  36. foreach (RoleAssignment grp in roleAssignments)
  37. {
  38. string strGroup = "";
  39. strGroup += grp.Member.Title +" : ";
  40. foreach (RoleDefinition rd in grp.RoleDefinitionBindings)
  41. {
  42. strGroup += rd.Name+ " ";
  43. }
  44. Console.WriteLine(strGroup);
  45. }
  46. Console.Read();
  47. }
  48. private static string getPassword()
  49. {
  50. ConsoleKeyInfo key;
  51. string pass = "";
  52. do
  53. {
  54. key = Console.ReadKey(true);
  55. // Backspace Should Not Work
  56. if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
  57. {
  58. pass += key.KeyChar;
  59. Console.Write("*");
  60. }
  61. else
  62. {
  63. if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
  64. {
  65. pass = pass.Substring(0, (pass.Length - 1));
  66. Console.Write("\b \b");
  67. }
  68. }
  69. }
  70. // Stops Receving Keys Once Enter is Pressed
  71. while (key.Key != ConsoleKey.Enter);
  72. return pass;
  73. }
  74. }
  75. }