0

I slightly modified this example of how to use CSOM to display a list's current views.

I'm getting the following error: "Microsoft.SharePoint.Client.CollectionNotInitializedException: '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.'"

Here is my code:

using (ClientContext cc = new ClientContext("https://demo.sharepoint.com/sites/SiteName"))
        {
            var passWord = new SecureString();
            foreach (char c in "password".ToCharArray()) passWord.AppendChar(c);

            cc.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);

            Web oWeb = cc.Web;
            List oList = oWeb.Lists.GetByTitle("Test List");
            ViewCollection oViewCollection = oList.Views;

            cc.Load(oWeb);
            cc.Load(oList);
            cc.Load(oViewCollection);

            cc.ExecuteQuery();

            Console.WriteLine(oWeb.Title);
            Console.WriteLine(oList.Title);

            foreach (View oneView in oViewCollection) //error occurs
            {
                Console.WriteLine(oneView.Title);
            }
            Console.ReadLine();
        }
    }

How can I resolve the error, so that I can loop through all of the view names and write them to the console?

2 Answers 2

0

try below code, while you load the view, you need to Include the fields you want to retrieve.

using (ClientContext cc = new ClientContext("https://demo.sharepoint.com/sites/SiteName"))
        {
            var passWord = new SecureString();
            foreach (char c in "password".ToCharArray()) passWord.AppendChar(c);

            cc.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);

            Web oWeb = cc.Web;
            List list = oWeb.Lists.GetByTitle("Test List");
            ViewCollection viewColl = list.Views;
            cc.Load(viewColl,
                views => views.Include(
                    view => view.Title,
                    view => view.Id)); 
            cc.ExecuteQuery();
            foreach (View view in viewColl)
            {
                Console.WriteLine(view.Title + "--------" + view.Id);
            }
            Console.ReadLine();
        }
    }
3
  • With that code, I get the same error as with my original code. Jan 23, 2020 at 17:06
  • Is your list name correct?? Jan 25, 2020 at 9:23
  • Yes, the list name is correct. I am able to output the list name. Jan 31, 2020 at 22:03
0

I had installed Microsoft.SharePoint.Client and SharePoint.Client.Runtime via NuGet, but I had not installed Microsoft.SharePointOnline.CSOM. Once I uninstalled the above packages and installed Microsoft.SharePointOnline.CSOM, the code ran correctly.

This article gives more details regarding which NuGet package to use for SP 2013, 2016 and Online.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.