Live training: next batches coming soon. Join the waitlist:Waitlists open:Developer Training →Developer →Copilot Studio Training →Copilot Studio →

The Collection Has Not Been Initialized in SharePoint CSOM: How to Fix It

A sales manager wanted a script that checks whether the SPGDemo Orders list has a Customer Name column. My first run stopped at list.Fields.Count with an exception that sounds worse than it is.

In this tutorial, we’ll reproduce the collection has not been initialized error and its property or field sibling in PowerShell 7, fix both with Load and ExecuteQuery, load items with CamlQuery, and cover JSOM and PnP PowerShell.

Quick answer: Call context.Load() on the collection or object, then context.ExecuteQuery() (or await ExecuteQueryAsync()) before you read Count, loop or read a property. In PnP PowerShell, Get-PnPProperty -ClientObject $obj -Property Fields does both steps.

Why SharePoint says the collection has not been initialized

CSOM queues GetByTitle() and Load() calls and sends nothing until ExecuteQuery() runs. Until then, list.Fields is an empty placeholder, and touching it throws CollectionNotInitializedException. My guide to CSOM in SharePoint Online explains the request model.

The collection has not been initialized error in PowerShell 7 when looping through an unloaded SharePoint Fields collection

PowerShell swallows exceptions from property getters, so .Count printed nothing; only the foreach loop surfaced the error. In C#, the code fails at list.Fields.Count:

var list = context.Web.Lists.GetByTitle("SPGDemo Orders");
for (int i = 0; i < list.Fields.Count; i++)   // CollectionNotInitializedException
{
    if (list.Fields[i].InternalName == "CustomerName") { Console.WriteLine("Column exists"); }
}

Fix it: load the collection, then execute the query

Load what you need, then send the request. Include() asks only for the field properties you read:

var list = context.Web.Lists.GetByTitle("SPGDemo Orders");
context.Load(list, l => l.Title, l => l.ItemCount);
context.Load(list.Fields, fields => fields.Include(f => f.InternalName, f => f.Title));
context.ExecuteQuery();   // await context.ExecuteQueryAsync() in CSOM for .NET Standard

bool exists = list.Fields.Any(f => f.InternalName == "CustomerName");
Console.WriteLine($"{list.Title}: {list.ItemCount} items, CustomerName exists: {exists}");

In PowerShell this returned 210 items and 95 fields, and found the Customer Name column. For a cmdlet-only version, see checking if a column exists with PnP PowerShell.

Fixing the collection has not been initialized error with ctx.Load and ExecuteQuery on the SPGDemo Orders list

The property or field ‘Url’ has not been initialized

PropertyOrFieldNotInitializedException is the single-property version: you read a property that was never requested, or loaded but never executed. I queued Load() for the root web and skipped ExecuteQuery(); calling the getter get_Url() shows the error PowerShell otherwise hides:

The property or field Url has not been initialized exception after Load without ExecuteQuery in PowerShell

In C#, this fails on the second line:

context.Load(context.Site.RootWeb, w => w.Id, w => w.Url);
string rootUrl = context.Site.RootWeb.Url;   // PropertyOrFieldNotInitializedException: 'Url'

Add ExecuteQuery() between the load and the read:

Web rootWeb = context.Site.RootWeb;
context.Load(rootWeb, w => w.Id, w => w.Url);
context.ExecuteQuery();
Console.WriteLine(rootWeb.Url);   // https://spguides.sharepoint.com/sites/SPGuides

Load list items with CamlQuery.CreateAllItemsQuery

CamlQuery.CreateAllItemsQuery(100) returns up to 100 items from every folder, but you still load and execute it. For filters, build the XML with a CAML query builder for SharePoint Online.

List orders = context.Web.Lists.GetByTitle("SPGDemo Orders");
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);   // RowLimit 100, all folders
ListItemCollection items = orders.GetItems(query);
context.Load(items, all => all.Include(i => i.Id, i => i["Title"], i => i["CustomerName"]));
context.ExecuteQuery();

foreach (ListItem item in items)
{
    Console.WriteLine($"{item.Id}  {item["Title"]}  {item["CustomerName"]}");
}
CamlQuery CreateAllItemsQuery loading SPGDemo Orders items and the property or field not initialized error for a missing ViewFields column

The second run asked ViewFields for Title only, so reading CustomerName threw “The property or field has not been initialized”. Request every column you read.

The same error in JSOM and PnP PowerShell

In JSOM, read results only inside the executeQueryAsync success callback, as in my JSOM examples for SharePoint:

var ctx = SP.ClientContext.get_current();
var items = ctx.get_web().get_lists().getByTitle("SPGDemo Orders")
    .getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items, "Include(Id, Title)");
ctx.executeQueryAsync(function () {
    console.log("Items loaded: " + items.get_count());   // read only inside the success callback
}, function (sender, args) { console.log(args.get_message()); });

In PnP PowerShell, it often appears as “format-default : The collection has not been initialized”. Load the property with Get-PnPProperty into a variable. New to the module? Start with installing PnP PowerShell.

$list = Get-PnPList -Identity "SPGDemo Orders"
$fields = Get-PnPProperty -ClientObject $list -Property Fields   # loads + runs the query
$fields.Count

Common errors and how to fix them

  • Load() without ExecuteQuery(): every batch of loads needs one execute before you read.
  • Unrequested property: w => w.Id loads only Id, and a plain Load(web) skips HasUniqueRoleAssignments.
  • SharePointOnlineCredentials: works only in CSOM for .NET Framework; CSOM for .NET Standard needs an Entra ID token, which PnP handles (see fixing Connect-PnPOnline is not recognized).

For more SharePoint code and scripts, try these next:

Frequently Asked Questions

What does “The collection has not been initialized” mean in SharePoint?

Your code read a collection, such as list.Fields, before SharePoint returned it. Load it and execute the query first.

How do I fix “The property or field has not been initialized”?

Request the property in Load, such as Load(web, w => w.Url), and call ExecuteQuery before reading it, as Microsoft’s CSOM basic operations guide shows.

What does CamlQuery.CreateAllItemsQuery do?

It builds a query for all items, including those in folders, up to the row limit you pass. You still load and execute the collection.

Why does PowerShell show nothing instead of the error?

PowerShell hides property getter exceptions, so unloaded properties return $null. Loops and getters such as get_Url() show the real error.

Is “CommandText property has not been initialized” the same error?

No. It’s an ADO.NET error for a SqlCommand without CommandText. Set the SQL text before ExecuteReader.

Leave a Comment