SOLVED

Add-SPOFile fails with format-default : The collection has not been initialized...

%3CLINGO-SUB%20id%3D%22lingo-sub-14323%22%20slang%3D%22en-US%22%3EAdd-SPOFile%20fails%20with%20format-default%20%3A%20The%20collection%20has%20not%20been%20initialized...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-14323%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20the%20PnP%20PowerShell%20CmdLets%20I'm%20trrying%20to%20add%20a%20Word%20document%20to%20a%20library.%20When%20I%20run%20the%20following%20line%20as%20part%20of%20a%20larger%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EAdd-SPOFile%20-Path%20%24path%2FDocumentTemplates%2FMyTemplate.docx%20-Folder%20DocumentTemplates%20-Web%20%24web%20-Values%20%40%7BTitle%3D%22My%20Template%22%3BFileLeafRef%3D%22MyTemplate.docx%22%7D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20i'm%20getting%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3Eformat-default%20%3A%20The%20collection%20has%20not%20been%20initialized.%20It%20has%20not%20been%20requested%20or%20the%20request%20has%20not%20been%20executed.%20It%20may%20need%20to%20be%20explicitly%20requested.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20running%20the%20PowerShell%20ISE%20and%20when%20I%20put%20a%20breakpoint%20on%20the%20above%20line%20and%20copy%2Fpaste%20%26nbsp%3Bthe%20above%20line%20in%20the%20Console%26nbsp%3Bsection%20then%20it%20runs%20wihtout%20failure.%20So%20the%20above%20variables%20(%24path%20and%20%24web%20are%20still%20set%20correctly%20as%20my%20debug%20session%20is%20still%20active)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20then%20continue%20with%20F10%20the%20script%20will%20fail%20with%20the%20same%20error.%20So%20it%20looks%20like%20there%20is%20a%20differnce%20between%20running%20this%20as%20part%20of%20a%20script%20or%20running%20Add-SPOFile%20from%20the%20console.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%20what%20the%20difference%20may%20be%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%2C%20I%20noticed%20that%20you%20answered%20question%20with%20the%20similar%20error%20on%20Github%2C%20i%20haven't%20found%20the%20same%20issue.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-14323%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-14333%22%20slang%3D%22en-US%22%3ERE%3A%20Add-SPOFile%20fails%20with%20format-default%20%3A%20The%20collection%20has%20not%20been%20initialized...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-14333%22%20slang%3D%22en-US%22%3EThanks%2C%20that%20fixed%20the%20issue.%20I%20understand%20that%20for%20PowerShell%20to%20present%20the%20object%20to%20me%20it%20isn't%20able%20to%20get%20to%20all%20of%20the%20data%20(not%20initialized).%20Still%20a%20bit%20unclear%20though%20why%20it%20would%20work%20when%20I%20run%20it%20from%20the%20Console%20in%20PowerShell%20ISE%20and%20not%20directly%20in%20a%20script.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-14325%22%20slang%3D%22en-US%22%3ERe%3A%20Add-SPOFile%20fails%20with%20format-default%20%3A%20The%20collection%20has%20not%20been%20initialized...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-14325%22%20slang%3D%22en-US%22%3E%3CP%3Eyou're%20not%20surpressing%20error%20messages%20in%20your%20script%3F%20As%20such%20it%20doesn't%20have%20to%20a%20be%20a%20blocking%20error.%20The%20cmdlet%20just%20outputs%20an%20object%20where%20one%20of%20the%20properties%20has%20not%20been%20loaded%20by%20CSOM.%20The%20object%20is%20still%20there.%20The%20simplest%20solution%20is%20to%20assign%20the%20output%20of%20the%20cmdlet%20into%20a%20variable%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24file%20%3D%20Add-SPOFile%20etc.%20etc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20will%20'swallow'%20the%20error%20message%20and%20put%20the%20resulting%20object%20into%20the%20%24file%20variable.%20You%20will%20receive%20the%20same%20error%20when%20you%20simply%20output%20the%20results%20of%20the%20%24file%20variable%20to%20the%20output.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-924143%22%20slang%3D%22en-US%22%3ERe%3A%20Add-SPOFile%20fails%20with%20format-default%20%3A%20The%20collection%20has%20not%20been%20initialized...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-924143%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%26nbsp%3BI%20see%20you%20fixed%20a%20similar%20issue%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F918%23issuecomment-372269506%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAdd-PnPFile%3C%2FA%3E%20cmdlet%2C%20would%20it%20be%20possible%20for%20you%20to%20commit%20a%20similar%20fix%20for%20the%20other%20SharePoint%20cmdlets%20(e.g.%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F799%23issuecomment-333802366%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EAdd-PnPListItem%20and%20%3C%2FA%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F799%23issuecomment-333802366%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3ESet-PnPListItem%3C%2FA%3E%20)%3F%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
MVP

Using the PnP PowerShell CmdLets I'm trrying to add a Word document to a library. When I run the following line as part of a larger script:

 

Add-SPOFile -Path $path/DocumentTemplates/MyTemplate.docx -Folder DocumentTemplates -Web $web -Values @{Title="My Template";FileLeafRef="MyTemplate.docx"}

 

Then i'm getting the following 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.

 

I'm running the PowerShell ISE and when I put a breakpoint on the above line and copy/paste  the above line in the Console section then it runs wihtout failure. So the above variables ($path and $web are still set correctly as my debug session is still active)

 

If I then continue with F10 the script will fail with the same error. So it looks like there is a differnce between running this as part of a script or running Add-SPOFile from the console.

 

Any ideas what the difference may be?

 

@Erwin van Hunen, I noticed that you answered question with the similar error on Github, i haven't found the same issue.

3 Replies
Highlighted
Best Response confirmed by Pieter Veenstra (MVP)
Solution

you're not surpressing error messages in your script? As such it doesn't have to a be a blocking error. The cmdlet just outputs an object where one of the properties has not been loaded by CSOM. The object is still there. The simplest solution is to assign the output of the cmdlet into a variable:

 

$file = Add-SPOFile etc. etc.

 

That will 'swallow' the error message and put the resulting object into the $file variable. You will receive the same error when you simply output the results of the $file variable to the output.

 

 

Highlighted
Thanks, that fixed the issue. I understand that for PowerShell to present the object to me it isn't able to get to all of the data (not initialized). Still a bit unclear though why it would work when I run it from the Console in PowerShell ISE and not directly in a script.
Highlighted

@Erwin van Hunen I see you fixed a similar issue in the Add-PnPFile cmdlet, would it be possible for you to commit a similar fix for the other SharePoint cmdlets (e.g. Add-PnPListItem and Set-PnPListItem )?