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.

In this SharePoint tutorial, I will show you how to solve a SharePoint error: 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. The error comes while working in CSOM (.Net managed object model (C#.net)), JSOM (JavaScript object model), or PnP SharePoint.

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.

Recently, I was working with SharePoint online csom code to check if a column exists in the SharePoint list.

We were using C#.Net object model code (csom), where we were using Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll inside a Windows form application.

My SharePoint CSOM code looks like below:

private void btnOnPremise_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
var list = context.Web.Lists.GetByTitle("MyDemoList");
for (int i = 0; i < list.Fields.Count; i++)
{
if (list.Fields[i].Title == "MyTestColumn")
{
label1.Text = "Column Exists";
return;
}
}
}
}

When I ran the above code, it gave the below error in “list.Fields.Count”. The error coming was “An unhandled exception of type ‘Microsoft.SharePoint.Client.CollectionNotInitializedException’ occurred in Microsoft.SharePoint.Client.Runtime.dll

Additional information: 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.” It looks like below:

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.
The collection has not been initialized SharePoint

If you are working with csom code, we need to load the properties before you try to use it. So, to use list.Fields.Count, we have to load the list.Fields first, and then we can use it. We also have to call the ExecuteQuery() method, not just the load() method.

So, I have modified the code like below. I have added the below two lines of code.

context.Load(list);
context.Load(list.Fields);

Below is the full SharePoint csom code:

private void btnOnPremise_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
var list = context.Web.Lists.GetByTitle("MyDemoList");
context.Load(list);
context.Load(list.Fields);
context.ExecuteQuery();
for (int i = 0; i < list.Fields.Count; i++)
{
if (list.Fields[i].Title == "MyTestColumn")
{
label1.Text = "Column Exists";
return;
}
}
}
}

The collection has not been initialized

“The collection has not been initialized” error will come if the object is not loaded and you are trying to access in your csom, jsom code.

Support you want to retrieve the list items from a particular list using jsom (announcement list ), then you need to load the items in csom SharePoint.

Like this: context.Load(items);

ClientContext context = new ClientContext("http://mypc:29024/sites/SPTraining");
List announcementsList = context.Web.Lists.GetByTitle("Announcements");

CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = announcementsList.GetItems(query);

context.Load(items);
context.ExecuteQuery();
foreach (ListItem listItem in items)
{
string s+= listItem["Title"].ToString();
}

If you do not lead items and try to retrieve list item Title, then the error will come as: The collection has not been initialized. It has not been requested or the request has not been executed.

SharePoint The collection has not been initialized (jsom)

Similarly, if you want to retrieve list items from a SharePoint list using jsom (javascript object model), you also need to load items collection to retrieve the properties.

function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
var collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(onsuccess, onfailure)
}

Here, you need to load collListItem before trying to access any item from the collection like the below:

clientContext.load(collListItem);

I hope this csom SharePoint tutorial will help to solve the problem. 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 error.

You may also like:

>