Retrieving all users from a SharePoint group using CSOM SharePoint Online

In this article you will learn to Retrieving all users from a SharePoint group using CSOM SharePoint Online.Also you can retrieve all groups of a Site Collection using client side object model(CSOM). Below example will show you to get all users of a group by passing group id.

using Microsoft.SharePoint.Client;
using System;
using System.Security;
 
namespace Retrieveallusersingroup
{
    class Program
    {
        static string Passwords = "Password";
        static string UserName = "UserName";
        static string SiteURL = "Sub Site URL";
        static void Main(string[] args)
        {
           
            using (ClientContext clientCont = new ClientContext(SiteURL))//PWA Url
            {
                SecureString passWord2 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord2.AppendChar(c);
                clientCont.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
 
 
                GroupCollection collGroup = clientCont.Web.SiteGroups;
                Group sGroup = collGroup.GetById(2);
                UserCollection siteUser = sGroup.Users;

                clientCont.Load(siteUser);

                clientCont.ExecuteQuery();

                foreach (User sUser in siteUser)
                   {
                Console.WriteLine("User: {0}  ID: {1} Email: {2} Login Name: {3}", 
                sUser.Title, sUser.Id, sUser.Email, sUser.LoginName);
                   }
               
            }
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.