In this post, we’ll see how we can retrieve users in a SharePoint group in SharePoint 2013 Online using managed .NET Client Side Object Model (CSOM). We’ll create a Console Application in Visual Studio for the purpose of the demo.
Open Visual Studio 2013.
File –> New Project –> Visual C# –> Console Application and name it as ‘GetUsersInGroup’
Add a reference to assemblies ‘Microsoft.SharePoint.Client’ and ‘Microsoft.SharePoint.Client.Runtime’.
Import the following two namespaces.
using Microsoft.SharePoint.Client; using System.Security;
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
In the client object model, the list groups in a site-collection needs to be fetched first and then based on the groups another call needs to be made to SharePoint Online to get the list of users. The following code snippet explains it all.
namespace GetUsersInGroupCSOM { class Program { static void Main(string[] args) { //Replace it with the url of your tenant or your site-collection string SiteUrl = "https://yoursite.sharepoint.com"; System.Uri oUri = new System.Uri(SiteUrl); using (ClientContext oClientContext = new ClientContext(SiteUrl)) { //Replace it with your user id for SharePoint Online string UserName = "lavsunswe@yoursite.onmicrosoft.com"; //Replace it with your password string Password = "yourpassword"; //Create a SecureString object from password string, needed for SharePointOnlineCredentials class SecureString SecurePassword = GetSecureString(Password); oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword); //Load the site-collection groups using CSOM oClientContext.Load(oClientContext.Web.SiteGroups); oClientContext.ExecuteQuery(); GroupCollection oSiteCollectionGroups= oClientContext.Web.SiteGroups; Console.WriteLine("List of groups in the site collection"); Console.WriteLine("-------------------------------------"); foreach (Group oGroup in oSiteCollectionGroups) { Console.WriteLine(oGroup.Title); Console.WriteLine("n"); } //Load the users collection in the Group 1 oClientContext.Load(oSiteCollectionGroups[1].Users); oClientContext.ExecuteQuery(); Console.WriteLine("List of users in the first group of site-collection"); Console.WriteLine("-------------------------------------"); foreach(User oUser in oSiteCollectionGroups[1].Users) { Console.WriteLine(oUser.Title); Console.WriteLine("n"); } Console.ReadLine(); } } private static SecureString GetSecureString(String Password) { SecureString oSecurePassword = new SecureString(); foreach (Char c in Password.ToCharArray()) { oSecurePassword.AppendChar(c); } return oSecurePassword; } } }
Nice piece of code, it really helped. Thanks. The msdn samples seem to be missing the Load method for various collections (followed by the executequery methhod). You examples helped piece it altogether for my coding problem. Thanks again.