Maciej
Maciej

Reputation: 55

format-default : The collection has not been initialized. PowerShell + SharePoint Online Error

I have a problem with my PowerShell script. Script removes records from SharePoint List and adds new records from csv afterwards. Basically when I run the script line by line all works fine. But when I try to run the whole thing I get this error:

format-default : 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. + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

Here's my code:

$list_modified = 'list_name'
$filename = "C:\.......\file.csv"

# Purge existing records in list
$records = Get-PnPListItem -List $list_modified -PageSize 500

# make sure fields in list have corresponding field in csv and same data type
Get-PnPField -List $list_modified

# purge list
foreach ($record in $records)
{
    Write-Host "Removing Id "  $record.Id
    Remove-PnPListItem -List $list_modified -Identity $record.Id -Force
}

Get-PnPField -List $list_modified
$records = Get-PnPListItem -List $list_modified -PageSize 500
$records | measure

# Import csv
$csv_data = Import-CSV -Path $filename -Delimiter `t

foreach ($row in $csv_data) {
  Add-PnPListItem -List $list_modified -Values @{
  "Title" = $row.'name';
  "uuid" = $row.'uuid';
  "name" = $row.'name';
  "short_description" = $row.'short_description';
  "logo_url" = $row.'logo_url';
  "homepage_url" = $row.'homepage_url';
  "category_groups_list" = $row.'category_groups_list';
  "category_list" = $row.'category_list';
  "total_funding_usd" = $row.'total_funding_usd';
  "last_funding_on" = $row.'last_funding_on'
  }
}

What am I doing wrong? Help please!

Thanks! Maciej

Upvotes: 0

Views: 1760

Answers (1)

Matthew
Matthew

Reputation: 21

A Google search for the following finds a number of similar issues:

format-default : The collection has not been initialized

from: https://github.com/pnp/PnP-PowerShell/issues/799#issuecomment-618926331

Try returning your statement into a variable.

Do this

$item = Add-PnPListItem -List $list -ContentType "Project" -Values $Values**

and

$item = Set-PnPListItem -List $list -Identity $targetItem.Id -Values $Values**

Upvotes: 2

Related Questions