Pnp-powershell: Get-PnpListItem errors when downloading list item attachment

Created on 6 Dec 2016  路  2Comments  路  Source: pnp/PnP-PowerShell

Notice: many issues / bugs reported are actually related to the PnP Core Library which is used behind the scenes. Consider carefully where to report an issue:

  1. Are you using Apply-SPOProvisioningTemplate or Get-SPOProvisioningTemplate? The issue is most likely related to the Provisioning Engine. The Provisioning engine is _not_ located in the PowerShell repo. Please report the issue here: https://github.com/officedev/PnP-Sites-Core/issues.
  2. Is the issue related to the cmdlet itself, its parameters, the syntax, or do you suspect it is the code of the cmdlet that is causing the issue? Then please continue reporting the issue in this repo.
  3. If you think that the functionality might be related to the underlying libraries that the cmdlet is calling (We realize that that might be difficult to determine), please first double check the code of the cmdlet, which can be found here: https://github.com/OfficeDev/PnP-PowerShell/tree/master/Commands. If related to the cmdlet, continue reporting the issue here, otherwise report the issue at https://github.com/officedev/PnP-Sites-Core/issues

Reporting an Issue or Missing Feature

GEt-PnPListItem returns an error when downloading List Attachments

Expected behavior

Expect to see the attachment file name

Actual behavior

Get an error
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.
At line:1 char:9

  • foreach($Attach in $AttachColl)
  • ~~~

    • CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException

    • FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException

Steps to reproduce behavior

Here is the code
Connect-PnpOnline -Url TenantUrl -Credentials (Get-Credential)
$item = Get-PnPListItem -List [ListUrl] -Query "

                <Eq>
                    <FieldRef Name='Title' /> 
                    <Value Type='Text'>[ItemTitle]</Value> 
                </Eq> 

            </Where></Query></View>"

$AttachColl = $item.AttachmentFiles

foreach($Attach in $AttachColl)
{
Write-Host $Attach.FileName
}

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

  • [ ] PnP PowerShell for SharePoint Online

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

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable)

How did you install the PnP-PowerShell Cmdlets?

  • [ ] MSI Installed downloaded from GitHub

Most helpful comment

Thanks Rene for answering this. Another approach you can use is

$attachments = Get-PnPProperty -ClientObject $item -Property "AttachmentFiles"

All 2 comments

Hi,

you need to request for the AttachmentFiles property to be loaded. Not all properties are loaded by default in CSOM, some of them you have to request manually.

Here's how to do it in your case (changes bold):

$AttachColl = $item.AttachmentFiles
$ctx = Get-PnPContext
$ctx.Load($AttachColl)
$ctx.ExecuteQuery()

foreach($Attach in $AttachColl)
{
Write-Host $Attach.FileName
}

Thanks Rene for answering this. Another approach you can use is

$attachments = Get-PnPProperty -ClientObject $item -Property "AttachmentFiles"
Was this page helpful?
0 / 5 - 0 ratings