pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
987 stars 665 forks source link

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

Open mrpullen opened 7 years ago

mrpullen commented 7 years ago

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 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?

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

2.11.1701.1

How did you install the PnP-PowerShell Cmdlets?

TomGoorts commented 7 years ago

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

berndverhofstadt commented 7 years ago

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

berndverhofstadt commented 7 years ago

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

DavidLozzi commented 7 years ago

Workaround worked, thanks!

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

shad0wbeast commented 7 years ago

Thanks berndverhofstadt.. It worked..

darrenparkinson commented 7 years ago

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

Leumas09 commented 7 years ago

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

marcomangiante commented 6 years ago

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?

andikrueger commented 6 years ago

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
christfarrugia commented 6 years ago

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.

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?

err-unknown-username commented 6 years ago

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.

fowl2 commented 6 years ago

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

simplysql commented 6 years ago

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

ToddKlindt commented 5 years ago

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.

AndyTuke commented 4 years ago

Same with the New-PNPTermGroup/Set cmdlets

khha commented 4 years ago

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

aronbardsley commented 4 years ago

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

vochitan90 commented 4 years ago

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!

tristian2 commented 4 years ago

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

harp365 commented 4 years ago

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

ghost commented 4 years ago

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

sathishvc commented 3 years ago

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