PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

format-default: the collection has not been initialized on Add-PnPFile after Apply-PnPProvisioning is Run

Open mrpullen opened this issue 8 years ago • 22 comments

Reporting an Issue or Missing Feature

Please confirm what it is that your reporting

I've written a PowerShell script (see below) to provision a pnp template then add a few files.

param(
    [Parameter(Mandatory=$true)]
    [string]$siteUrl,
)

$path = Split-Path -Parent $MyInvocation.MyCommand.Path
**Connect-PnPOnline** -Url $siteUrl -CurrentCredentials
$templateFile = Join-Path $path "template.xml"
$csomFiles = Join-Path $path "CSOM"
**Apply-PnPProvisioningTemplate** -Path $templateFile -Handlers All

$files = Get-ChildItem -Path $csomFiles -Recurse
foreach($file in $files) {
    Write-Host "Adding file $($file.BaseName) to Site Folder /CSOM"
    **Add-PnPFile** -Path "$($file.FullName)" -Folder "/CSOM"
}

This worked properly until I added a pnp:Page into the provisioning template. Afterthat I started to receive the following error.

Adding file bundle to Site Folder /CSOM
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

Now after the error if I just select the powershell (in powershell ISE)

foreach($file in $files) {
    Write-Host "Adding file $($file.BaseName) to Site Folder /CSOM"
    **Add-PnPFile** -Path "$($file.FullName)" -Folder "/CSOM"
}

and re-run that piece it completes without error. Seems like the works of the context are being "gummed up" somehow.

Expected behavior

Would like the Add-PnpFile to operate as expected regardless of what commands it follows.

Actual behavior

screen-clip

Steps to reproduce behavior

(Shown above)

Which version of the PnP-PowerShell Cmdlets are you using?

  • [ X ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [ ] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

2.11.1701.1

How did you install the PnP-PowerShell Cmdlets?

  • [ X ] MSI Installed downloaded from GitHub
  • [ ] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means

mrpullen avatar Jan 31 '17 23:01 mrpullen

Same problem here with adding list-items after applying the template. I'm using SPO, and installed via Install-Module.

TomGoorts avatar Feb 14 '17 08:02 TomGoorts

Same issue. I'm using PnP PowerShell for SharePoint Online

berndverhofstadt avatar Feb 20 '17 16:02 berndverhofstadt

I've fixed this by adding a variable in front $file = Add-PnPFile -Path "$($file.FullName)" -Folder "/CSOM" I've found this on Microsoft's Tech Community

berndverhofstadt avatar Feb 20 '17 17:02 berndverhofstadt

Workaround worked, thanks!

$file = Add-PnPFile -Path "client_LocationBasedDefaults.html" -Folder "documents/forms"

DavidLozzi avatar Mar 08 '17 20:03 DavidLozzi

Thanks berndverhofstadt.. It worked..

shad0wbeast avatar Mar 25 '17 09:03 shad0wbeast

I also experienced something similar with Add-PnPView. Adding $view = at the beginning appears to have fixed it. This is quite worrying behaviour isn't it?

$view = Add-PnPView -List $Url -SetAsDefault -Title "All Documents" -Fields DocIcon,LinkFilename,Modified,Editor

darrenparkinson avatar Apr 26 '17 11:04 darrenparkinson

Hello, I had the same issue with Set-PnPListItem with the variable in front, it works !

$updatedItem = Set-PnPListItem -List $projectListName -Identity $IDItem -Values $itemValues

Thanks

Leumas09 avatar Jul 17 '17 09:07 Leumas09

Same problem here on Add-PnpFile that I can't resolve with the workaround suggested; I have a simple script that uses windows backup cmdlet and then try to copy the result on SharePoint. If I use the only part of the script related to copy the files backed up I have no issue. I can attach a file with the entire script?

marcomangiante avatar Oct 18 '17 12:10 marcomangiante

I see the same problem. Using Add-PnpFile without assigning the output to a variable, this command fails.

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.FormatDe 
   faultCommand

andikrueger avatar Nov 02 '17 13:11 andikrueger

I am quite new to Powershell, and trying out a few automations.

I have the same problem using the Add-PnPFile: Add-SPOFile -Path "pathofthefileonlocalstorage" -Folder "SiteAssets" 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

However running the exact same line from the command line executes without any issue.

I also tried the workaround: $file = Add-SPOFile -Path "pathofthefileonlocalstorage" -Folder "SiteAssets" Same error

Any ideas please?

christfarrugia avatar Nov 30 '17 18:11 christfarrugia

I have a script which uploads multiple files to a SPOnline library; as such it calls Add-PnPFile a bunch or times.

Without the assignment it works for a while (somewhere around 10 to 30 files) before failing with the above error. With the assignment it appears to complete for all file.

err-unknown-username avatar Feb 19 '18 22:02 err-unknown-username

This can also occur when using Ensure-PnPFolder, workaround is the same.

fowl2 avatar Mar 01 '18 06:03 fowl2

the $file fixed it for me - not sure why? but I'm back in business - thank you!

simplysql avatar Sep 12 '18 13:09 simplysql

I just needed this for ConvertTo-PnPClientSidePage. With one set of pages it would run fine. With another set of pages it would error out with the "format-default..." error. Prefacing the command with "$test = " fixed it right up.

ToddKlindt avatar Jun 15 '19 21:06 ToddKlindt

Same with the New-PNPTermGroup/Set cmdlets

AndyTuke avatar Sep 25 '19 12:09 AndyTuke

I've got the same error when I use Apply-PnPProvisioningTemplate and try to create a new clienstSidePage:

pnp:ClientSidePages <pnp:ClientSidePage PageName="Home.aspx".....

Sometimes I got this error, sometimes I do not get this error.

Please help!

khha avatar Oct 18 '19 16:10 khha

So I understand that in PowerShell - if i call a PnP function (like Add-PnpView) and I don't store the return value in a variable, PowerShell's default behaviour is to output the returned value in the console.

Of course, PowerShell - in doing this - attempts to read the returned object's properties. The object returned is coming from PnP PowerShell - and upon accessing the properties of the object, PnP throws an exception saying it is uninitialised.

It only happens occasionally, making me think it's some sort of race condition when calling functions inside the PnP framework - like the query is executed, but the function returns before it's data is made fully available to the return object.

The thing is, the command still executes... but the error stops my entire script. I run long PowerShell scripts to make deployments to SharePoint. I can say without a doubt, this happens at least once every time i run a script - at different points in the script each time.

Short of putting $result = every time I use a PnP command, I'm at the point now, where i'm considering a nuclear option like this: function out-default {$input | out-null}

I don't have time to test it right now, since this rant is caused by me running a deployment script I need to finish - and of course, when i want to reproduce the problem it magically goes away. But it would be really nice if the underlying problem was looked at seriously and fixed.

Thanks

aronbardsley avatar Nov 14 '19 03:11 aronbardsley

Same here when I use this cmd to create a folder

Resolve-PnPFolder -SiteRelativePath $relativePath -ErrorAction Stop;

My current version is "SharePointPnPPowerShellOnline 3.1.1809.0"

Do I need to update version for PNP Powershell?

Thanks!

vochitan90 avatar May 22 '20 12:05 vochitan90

hi,

this issue started happening to me today. it is happening in a script that was tested and working. my particular issue is that if i run Add-PnPListItem -List etc

i get the error and execution halts. adding like so resolves it $item = Add-PnPListItem -List

all i can think of is that i upgraded PNP a few weeks back

running Get-Module SharePointPnPPowerShell* -ListAvailable | Select-Object Name,Version | Sort-Object Version -Descending

i see Name Version


SharePointPnPPowerShellOnline 3.21.2005.1

thanks

tristian2 avatar Jun 09 '20 15:06 tristian2

It will no doubt be because one of the returned values once completed, has not been initialised. A bit like when you need a property and it is not present upfront. You can either use the Includes when available or get-pnpcontext, load the object into the context and then execute but I think I will go with the variable as the command does actually work

harp365 avatar Jun 12 '20 16:06 harp365

Same issue with Add-PnPView. Works if I Out-Null the line.

ghost avatar Jul 07 '20 14:07 ghost

Thank you @Leumas09. That fixed it for me too!

sathishvc avatar Nov 09 '20 16:11 sathishvc