Get SharePoint User Group Names in .NET/C# using CSOM

Introduction

Here we will discuss an important aspect of dealing with SharePoint user groups from a .net client application. Although examples given here are in C# but those can be easily converted to VB.NET.

Sample Cases

This has two flavours. If our program is running under an account that has access to that site, it is pretty straight forward. However, if you are running the application under an account which does not have permission to that site, then we have to provide a mechanism in the code to connect using a different account (assumed it has the right access). For example, in development environment we are running the application for testing. In that case, it will run under the developer's log-in credential. But if our code is running as a part of a code behind in a web application, when deployed in production, the code will run under the credential of the IIS' application pool's account. 

Please ensure you included the following name spaces in your code file.

using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client; // This comes from Microsoft.SharePoint.Client.dll. 
 
/* We can download this from the following URL for the development machines where no SharePoint is installed.
SharePoint 2010: http://www.microsoft.com/en-us/download/details.aspx?id=21786 
SharePoint 2013: http://www.microsoft.com/en-in/download/details.aspx?id=35585 ; 
*/

Scenario 1: Application is running under the credential which has access to the SharePoint site.

string url = "http://sharepointsiteurl.com";
List<string> spgs = null; // string list to hold the group names in a given SharePoint site.
  
                using (ClientContext ctx = new ClientContext(url))
                {                 
                    GroupCollection grps = ctx.Web.SiteGroups;                   
                    ctx.Load(grps, groups => groups.Include(group => group.Title));
                    ctx.ExecuteQuery(); //LINQ executes here and hits the site to get the data
  
                    spgs = new  List<string>();
  
                    foreach (Group g in grps)
                        spgs.Add(g.Title);
  
                    if (spgs != null) spgs.Sort(); // Sort the group names
                }

Scenario 2: Application is running by providing connection account credential to the SharePoint site.

string url = "http://sharepointsiteurl.com";
  
List<string> spgs = null; // string list to hold the group names in a given SharePoint site.
  
                using (ClientContext ctx = new ClientContext(url))
                {
                    // Following line sets the credential.
                    ctx.Credentials = new System.Net.NetworkCredential("MyServiceAccount",
"MyServiceAccountPassword","MyServiceAccountDomain");
  
                    GroupCollection grps = ctx.Web.SiteGroups;                   
  
                    ctx.Load(grps, groups => groups.Include(group => group.Title));
                    ctx.ExecuteQuery(); //LINQ executes here and hits the site to get the data
  
                    spgs = new  List<string>();
  
                    foreach (Group g in grps)
                        spgs.Add(g.Title);
  
                    if (spgs != null) spgs.Sort(); // Sort the group names
                }

This NetworkCredential() accepts account name in three parts. Say, our connection account is like mydomain\myaccount and uses password mypass. The function call will look like

ctx.Credentials = new  System.Net.NetworkCredential("myaccount", "mypass",  "mydomain");

There are many properties in the group of GroupCollection. We can fetch anything by choosing required property. Here we will conclude with just 'name'. Happy Coding!

References