Get-PnPListItem with CAML Query doesn't work

David Jenkins 946 Reputation points
2021-10-18T14:01:03.947+00:00

I'm trying to understand why the CAML I'm using isn't working with the Sharepoint PNP Commands. These commands work in my query builder and I've used them in the past with older PowerShell code so I'm fairly confident something should work.

I'm trying to recurs directories and find files. It seems like no matter what I do with Get-PnPListItem it doesn't honor all the markup. I can't even get it to find a title properly. It always finds folders. I tried <View Scope='FileOnly' /> but it is ignored. If I use RecuresALL I get all and the query is ignored.

Is get-pnplistitem with CAML broken?

It is so hard to find examples and proper docs on it. It was hard to figure out where to go for PNP Questions.

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. David Jenkins 946 Reputation points
    2021-10-18T15:00:07.113+00:00

    I got it to work.

    single file

    $camlQuery = "<View Scope="RecursiveAll">
    <Query>
    <Where>
    <Eq>
    <FieldRef Name = 'FileLeafRef'/>
    <Value Type = 'Text'>{0}</Value>
    </Eq>
    </Where>
    </Query>
    </View>" -f "filename.docx"
    $camlQuery

    begins with

    $camlQuery = "<View Scope="RecursiveAll">
    <Query>
    <Where>
    <And>
    <BeginsWith>
    <FieldRef Name='FileLeafRef' />
    <Value Type='File'>{0}</Value>
    </BeginsWith>
    <Eq>
    <FieldRef Name='FSObjType' />
    <Value Type='Integer'>0</Value>
    </Eq>
    </And>
    </Where>
    </Query>
    </View>" -f "filename"
    $camlQuery

    $items = Get-PnPListItem -List "Documents" -Query $camlQuery
    $items

    foreach($item in $items){

    Get-PnPFile -Url "$($item["FileRef"])" -Path C:\scripts -FileName "$($item["FileLeafRef"])" -AsFile -Force
    

    }

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.