-1

I have been trying to get all the users from the SP site. I have the code working for getting the groups adn group names but am getting an exception when I try to access the users from the groups. I initialized all the components before I used it and I think this is a classical case of CSOM hell. But even after a lot of changes I was not able to solve it. Any inputs are appreciated..

Exception Details:

Microsoft.SharePoint.Client.CollectionNotInitializedException was caught HResult=-2146233079 Message=The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. Source=Microsoft.SharePoint.Client.Runtime StackTrace: at Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext() at GetUsersInGroupCSOM.Program.Main(String[] args) InnerException:

Code"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;

namespace GetUsersInGroupCSOM
{

    class Program
    {
        static void Main(string[] args)
        {
            //Replace it with the url of your tenant or your site-collection
            string SiteUrl = "The Website";

            System.Uri oUri = new System.Uri(SiteUrl);

            using (ClientContext oClientContext = new ClientContext(SiteUrl))
            {
                //Replace it with your user id for SharePoint Online
                string UserName = XXXXXXX;


                //Replace it with your password
                string Password = XXXXXXXXXXXXXX;

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                //SecureString SecurePassword = GetSecureString(Password);

                //Old Credential code
                //oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                oClientContext.Credentials = new NetworkCredential(UserName, Password);

                //Load the site-collection groups using CSOM     
                Console.WriteLine(oClientContext.Web.SiteGroups);

                oClientContext.Load(oClientContext.Web.SiteGroups);

                try
                {
                    oClientContext.ExecuteQuery();
                    Console.WriteLine("Connected");
                }
                catch (Exception e)
                {

                    Console.WriteLine("The error is \n" + e.Message);
                    Console.WriteLine("The source is \n" + e.Source);
                    Console.WriteLine("The Stack Trace is \n" + e.StackTrace);
                }
                GroupCollection oSiteCollectionGroups = oClientContext.Web.SiteGroups;
                oClientContext.Load(oSiteCollectionGroups);
                Console.WriteLine("List of groups in the site collection");
                Console.WriteLine("-------------------------------------");

                Console.WriteLine(oSiteCollectionGroups.AreItemsAvailable);

                foreach (Group oGroup in oSiteCollectionGroups)
                {

                    Console.WriteLine(oGroup.Title);

                }
                Console.WriteLine("<<<<<<<<<<===================|End of Groups|========================>>>>>>>");
                //Load the users collection in the Group 1



                Console.WriteLine(oSiteCollectionGroups[1].Users);

                oClientContext.Load(oSiteCollectionGroups[1].Users);


                try
                {
                    oClientContext.ExecuteQuery();
                    Console.WriteLine("Got the users >-->");
                }
                catch (Exception e)
                {
                    Console.WriteLine("The error is \n" + e.Message);
                    Console.WriteLine("The source is \n" + e.Source);
                    Console.WriteLine("The Stack Trace is \n" + e.StackTrace);
                }

                Console.WriteLine("List of users in the first group of site-collection");
                Console.WriteLine("-------------------------------------------------------");
                //Console.WriteLine(oSiteCollectionGroups[1].Users);
                //Console.WriteLine(oSiteCollectionGroups[1].AllowMembersEditMembership);
                //Console.WriteLine(oSiteCollectionGroups[1].CanCurrentUserViewMembership);
                //Console.WriteLine(oSiteCollectionGroups[1].Context);
                //Console.WriteLine(oSiteCollectionGroups[1].Description);
                //Console.WriteLine(oSiteCollectionGroups[1].LoginName);
                //Console.WriteLine(oSiteCollectionGroups[1].Title);
                //Console.WriteLine(oSiteCollectionGroups[1].ToString());

                for (int i = 1; i < oSiteCollectionGroups.Count; i++)
                {
                    //Group oGroup = oClientContext.Web.SiteGroups.GetById(i);
                    oClientContext.Load(oSiteCollectionGroups[i].Users);
                    Console.WriteLine("Items Available =>"+oSiteCollectionGroups[i].Users.AreItemsAvailable);
                    Console.WriteLine(oSiteCollectionGroups[i].Description);
                    Console.WriteLine(oSiteCollectionGroups[i].LoginName);
                    Console.WriteLine(oSiteCollectionGroups[i].Title);
                    Console.WriteLine("Owner Title =>"+oSiteCollectionGroups[i].OwnerTitle);

                    oClientContext.Load(oSiteCollectionGroups[i].Users);

                    try
                    {
                        foreach (User oUser in oSiteCollectionGroups[i].Users)
                       {
                         Console.WriteLine(oUser.Title);
                         Console.WriteLine("n");
                       }

                    }
                    catch (Exception e)
                    {

                        Console.WriteLine("Exception Occured");
                        Console.WriteLine("The error is \n" + e.Message);
                        Console.WriteLine("The source is \n" + e.Source);
                        Console.WriteLine("The Stack Trace is \n" + e.StackTrace);
                    }
                }
                Console.WriteLine("<<<<<<<<<<===================|End of Memebers|========================>>>>>>>");
                Console.ReadLine();

            }



        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

1 Answers1

0

You have to load the Properties in CSOM.

string SiteUrl = "The Website";

        System.Uri oUri = new System.Uri(SiteUrl);

        using (ClientContext oClientContext = new ClientContext(SiteUrl))
        {
            //Replace it with your user id for SharePoint Online
            string UserName = XXXXXXX;
            //Replace it with your password
            string Password = XXXXXXXXXXXXXX;
            oClientContext.Credentials = new NetworkCredential(UserName, Password);
            Console.WriteLine(oClientContext.Web.SiteGroups);
            oClientContext.Load(oClientContext.Web, w => w.SiteGroups.Include(o => o.Users.Include(l => l.LoginName), o => o.Title, o => o.Description, o => o.OwnerTitle));
            try
            {
                oClientContext.ExecuteQuery();
                Console.WriteLine("Connected");
            }
            catch (Exception e)
            {

                Console.WriteLine("The error is \n" + e.Message);
                Console.WriteLine("The source is \n" + e.Source);
                Console.WriteLine("The Stack Trace is \n" + e.StackTrace);
            }
            GroupCollection oSiteCollectionGroups = oClientContext.Web.SiteGroups;              
            Console.WriteLine("List of groups in the site collection");
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(oSiteCollectionGroups.AreItemsAvailable);
            foreach (Group oGroup in oSiteCollectionGroups)
            {
                Console.WriteLine(oGroup.Title);
            }
            Console.WriteLine("<<<<<<<<<<===================|End of Groups|========================>>>>>>>");                
            Console.WriteLine("List of users in the first group of site-collection");
            Console.WriteLine("-------------------------------------------------------");              

            for (int i = 1; i < oSiteCollectionGroups.Count; i++)
            {
                Console.WriteLine("Items Available =>" + oSiteCollectionGroups[i].Users.AreItemsAvailable);
                Console.WriteLine(oSiteCollectionGroups[i].Description);
                Console.WriteLine(oSiteCollectionGroups[i].LoginName);
                Console.WriteLine(oSiteCollectionGroups[i].Title);
                Console.WriteLine("Owner Title =>" + oSiteCollectionGroups[i].OwnerTitle);                    
                try
                {
                    foreach (User oUser in oSiteCollectionGroups[i].Users)
                    {
                        Console.WriteLine(oUser.Title);
                        Console.WriteLine("n");
                    }

                }
                catch (Exception e)
                {

                    Console.WriteLine("Exception Occured");
                    Console.WriteLine("The error is \n" + e.Message);
                    Console.WriteLine("The source is \n" + e.Source);
                    Console.WriteLine("The Stack Trace is \n" + e.StackTrace);
                }
            }
            Console.WriteLine("<<<<<<<<<<===================|End of Memebers|========================>>>>>>>");
            Console.ReadLine();

        }

Try this.

Naveen Prasath
  • 539
  • 1
  • 3
  • 23