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