0

I am trying to get an item in a SharePoint list using a CAML query so that I can update it. I am using data in a CSV file but when I run the script, it's failing on Get-PnPListItem. Based on some research I believe I'm getting this error because I have an error with the query. Does anyone know what the error is? The description field may be null in CSV which is why there's a conditional. Title and Location always have a value.

$data= Import-Csv "myPath\data.csv"

foreach ($item in $data) {
    $query = "<View><Query><Where><And><Eq><FieldRef Name='Title'/><Value Type='Text'>" + $item.Title + "</Value></Eq><Eq><FieldRef Name='Location'/><Value Type='Text'>" + $item.Location + "</Value></Eq>"

    if($item.Description -eq $null) {
        $query += "<IsNull><FieldRef Name='Description'/></IsNull>"
    } else {
        $query += "<Eq><FieldRef Name='Description'/><Value Type='Text'>" + $item.Description + "</Value></Eq>"
    }

    $query += "</And></Where></Query></View>"

    $spListItem = Get-PnPListItem -List "My SP List" -Query $query
}

Here is an example of what the query string looks like:

<View><Query><Where><And><Eq><FieldRef Name='Title'/><Value Type='Text'>Grand Canyon</Value></Eq><Eq><FieldRef Name='Location'/><Value Type='Text'>Arizona</Value></Eq><Eq><FieldRef Name='Description'/><Value Type='Text'>National Park</Value></Eq></And></Where></Query></View>

I have verified internal names are properly spelled. All field types in query are single line of text

0

Your CAML query is invalid. In your example, you are trying to add 3 child elements to the And tag, while it only can have two. It means you are missing one more < And > tag:

Instead of this

<View>
    <Query>
        <Where>
            <And>
                <Eq>
                    <FieldRef Name='Title'/>
                    <Value Type='Text'>Title</Value>
                </Eq>
                <Eq>
                    <FieldRef Name='Location'/>
                    <Value Type='Text'>Location</Value>
                </Eq>   
                <!-- AND CANNOT HAVE 3 CHILD ELEMENTS: -->
                <Eq><FieldRef Name='Description'/><Value Type='Text'>Description</Value></Eq>         
            </And>
        </Where>
    </Query>
</View>

You need an extra And condition like so:

<View>
    <Query>
        <Where>
            <And>
                <And>
                    <Eq>
                        <FieldRef Name='Title'/>
                        <Value Type='Text'>Title</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Location'/>
                        <Value Type='Text'>Location</Value>
                    </Eq>                    
                </And>              
                <Eq><FieldRef Name='Description'/><Value Type='Text'>Description</Value></Eq>       
            </And>
        </Where>
    </Query>
</View>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.