问题
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