检索Sharepoint列表C#

时间:2017-02-08 14:22:29

标签: c# sharepoint

我试图检索SharePoint列表及其中的所有项目,但我似乎无法获取列表。我可以很好地连接到网站,但是当我尝试检索列表时,我得到一个" CollectionNotInitialized"错误。我以管理员身份登录并拥有完全控制权限。任何人都可以帮我解释这个问题吗?这是我的代码

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

答案 0 :(得分:2)

您可以尝试以下代码:

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