How to Retrive All Groups in SharePoint Programmatically

Steps
  • Open Visual Studio in your system
  • Select Console Applciation template and give as name .
  • Add a Microsoft.Cleint Assembly refrence file in right side refrence tab in visual studio.
  • Replace Program.cs with the source code  
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6. using Microsoft.SharePoint.Client;    
  7. using System.IO;    
  8. namespace CSOMCodeSamples    
  9. {    
  10.    class Program    
  11.    {    
  12.       static void Main(string[] args)    
  13.       {    
  14.          // ClientContext - Get the context for the SharePoint Site    
  15.     
  16.          ClientContext clientContext = new ClientContext("http://gauti.sharepoint/sites/CSOM/");    
  17.          // Get the SharePoint web    
  18.          Web web = clientContext.Web;    
  19.          // Get all the site groups    
  20.          GroupCollection groupColl = web.SiteGroups;    
  21.          clientContext.Load(groupColl);    
  22.          // Execute the query to the server    
  23.          clientContext.ExecuteQuery();    
  24.          // Loop through all the groups    
  25.          foreach (Group group in groupColl)    
  26.          {    
  27.             // Display the group name    
  28.             Console.WriteLine(group.Title);    
  29.          }    
  30.          Console.ReadLine();    
  31.       }    
  32.    }    
  33. }     
Hit F5 and check the output .