PnP-PowerShell
PnP-PowerShell copied to clipboard
format-default: the collection has not been initialized on Add-PnPFile after Apply-PnPProvisioning is Run
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
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
Same problem here with adding list-items after applying the template. I'm using SPO, and installed via Install-Module.
Same issue. I'm using PnP PowerShell for SharePoint Online
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
Workaround worked, thanks!
$file = Add-PnPFile -Path "client_LocationBasedDefaults.html" -Folder "documents/forms"
Thanks berndverhofstadt.. It worked..
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
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
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?
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
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?
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.
This can also occur when using Ensure-PnPFolder
, workaround is the same.
the $file fixed it for me - not sure why? but I'm back in business - thank you!
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.
Same with the New-PNPTermGroup/Set cmdlets
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!
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 = 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
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!
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
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
Same issue with Add-PnPView. Works if I Out-Null the line.
Thank you @Leumas09. That fixed it for me too!