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.

This SharePoint tutorial we will discuss 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 also.

Recently I was working with SharePoint online csom code to check if a column exists in SharePoint Online or SharePoint 2013/2016 list or not.

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.

You can see the SharePoint tutorial: SharePoint 2016 CSOM Check if Column Exists or Not in SharePoint List.

[toc]

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

The collection has not been initialized. It has not been requested or the request has not been executed

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

So I have modified the code like below, basically, 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 below:

clientContext.load(collListItem);

You may like following SharePoint csom tutorials:

Tags: The collection has not been initialized, The collection has not been initialized SharePoint online, The collection has not been initialized SharePoint 2013 c#, The collection has not been initialized SharePoint 2013 csom, The collection has not been initialized SharePoint 2013, the property or field has not been initialized SharePoint client object model, Microsoft.sharepoint.client.propertyorfieldnotinitializedexception c#, SharePoint 2013 the property or field has not been initialized, the property or field has not been initialized csom, the property or field has not been initialized SharePoint, the collection has not been initialized SharePoint 2013 c#, csom the collection has not been initialized, the collection has not been initialized SharePoint online, SharePoint client object model the collection has not been initialized, SharePoint clientcontext the collection has not been initialized, SharePoint the property or field id has not been initialized, the property or field has not been initialized c#.

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

>