SOLVED: Add-PnPFile: 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.

SOLVED: Add-PnPFile: 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.

  •  
  •  
  •  
  •  
  •  
  •  

While hammering out a script for one of my customers today, I decided that it would be a nice touch to upload a log file (that I’d handily built with this function) to their SharePoint team site.

What could go wrong?

Turns out, a lot.

Background

I’d already used Get-PnPFile to download the source file from the site, and had the PnP-PowerShell cmdlets loaded.

I tested the syntax interactively and made sure it worked.

Sweet! Now time to just add that line to the end of my script.

And then it hit me:

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

Resolution

I tried all the normal stuff:

  • wrapping it in a try/catch block
  • sleeping
  • setting the ErrorAction to SilentlyContinue
  • setting the ErrorAction to Ignore
  • swearing
  • Disconnecting and reconnecting from the PnP endpoint
  • rebooting
  • wrapping the cmdlet in a function
  • throwing my keyboard
  • kicking my desk
  • swearing more loudly

All to no avail.

This error not only terminated the cmdlet–it terminated the entire script.  I had wrapped my Add-PnPFile cmdlet inside a try/catch block, which was nested inside another try/catch block.  Everything just quit.  Do not pass go. Do not collect $200.

In the end, the solution was a simple one (albeit a frustrating one with literally no explanation):

Cast it to a variable.

That’s it.

Instead of:

Try { Add-PnPFile -Path $LogFile -Folder $SharePointOnlineLogPath }

it became

Try { $Upload = Add-PnpFile -Path $LogFile -Folder $SharePointOnlineLogPath }

Frustrating, but it let me continue.

Go into IT, they said.  It will be fun, they said.

I hope you never encounter this, but if you do, you can give this trick a try.

Published by Aaron Guilmette

Helping companies conquer inferior technology since 1997. I spend my time developing and implementing technology solutions so people can spend less time with technology. Specialties: Active Directory and Exchange consulting and deployment, Virtualization, Disaster Recovery, Office 365, datacenter migration/consolidation, cheese.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.