如何从共享点获取所有用户站点中的组。

时间:2017-08-02 21:14:17

标签: c# exception sharepoint csom

我一直在尝试从SP网站获取所有用户。我有代码工作,以获取组和组名称,但当我尝试从组中访问用户时,我得到一个例外。我在使用它之前初始化了所有组件,我认为这是CSOM地狱的经典案例。但即使经过很多改变,我也无法解决它。任何投入都表示赞赏..

例外详细信息:

发现了Microsoft.SharePoint.Client.CollectionNotInitializedException   的HResult = -2146233079   消息=尚未初始化集合。尚未请求或请求尚未执行。可能需要明确请求。   来源= Microsoft.SharePoint.Client.Runtime   堆栈跟踪:        在Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext()        at GetUsersInGroupCSOM.Program.Main(String [] args)   InnerException:

代码"

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 个答案:

答案 0 :(得分:0)

您必须在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();

        }

试试这个。