Retrieving Sharepoint List C#

时间秒杀一切 提交于 2021-02-10 12:15:08

问题


I'm trying to retrieve a SharePoint list and all the items within it, however I CANNOT seem to grab the list. I can connect to the site fine, but when I try to retrieve the list, I get a "CollectionNotInitialized" error. I'm logged in as admin and have full control permissions. Can anyone help explain this issue to me? Here is my code

 static void Main(string[] args)
    { 
        try
        { 
            string userName = "//my user name";
            string password = "//my password";
            SecureString ssPwd = new SecureString();

            foreach(char c in password.ToCharArray())
            {
                ssPwd.AppendChar(c);
            }

            CamlQuery query = CamlQuery.CreateAllItemsQuery();
            ClientContext context = new ClientContext("https://my fake site.com");
            SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, ssPwd);
            context.Credentials = credentials;


            Web web = context.Web;
            context.Load(web.Lists);
            Microsoft.SharePoint.Client.List list = context.Web.Lists.GetByTitle("Accounts");
            context.Load(list);

            context.ExecuteQuery();



        }
        catch
        {
           // Console.WriteLine();

回答1:


You can try the following code:

    var lists = web.Lists;
    context.Load(lists, all => all
      .Where(l => l.RootFolder.Name == "Accounts")
      .Include(l => l.Id));
    context.ExecuteQuery();
    list = lists.Single();


来源:https://stackoverflow.com/questions/42115675/retrieving-sharepoint-list-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!