I am working with SharePoint Online and want to build a query with PnP PowerShell that checks if any dates in the date field Aviseringsdatum
is equal to todays date. But my code still returns all items (currently 2 items in the list. One with the date today and one with the date yesterday). Any ideas?
The Code
$Today = (Get-date).ToString( "yyyy-MM-ddTHH:mm:ss.fffffffZ" )
$ListItems = Get-PnPListItem -List "Avtal" -Query "
<Query>
<Where>
<eq>
<FieldRef Name='Aviseringsdatum' />
<Value IncludeTimeValue='TRUE' Type='DateTime'>$Today</Value>
</eq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='Aviseringsdatum' />
<FieldRef Name='Avtalsansvarig' />
</ViewFields>
<QueryOptions />
";
You are missing the <View>
attribute in CAML query.
PnP PowerShell internally uses CSOM.
To query list items in CSOM, you need <View>
attribute.
So, modify your code as below:
$Today = (Get-date).ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")
$camlQuery = $("<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Aviseringsdatum' />
<Value IncludeTimeValue='TRUE' Type='DateTime'>$Today</Value>
</Eq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='Aviseringsdatum' />
<FieldRef Name='Avtalsansvarig' />
</ViewFields>
</View>")
$ListItems = Get-PnPListItem -List "Avtal" -Query $camlQuery
You can also, replace the line to use the out of the box <Today/>
element as below:
<Value IncludeTimeValue='TRUE' Type='DateTime'><Today /></Value>
so, your full code would be like :
$camlQuery = $("<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Aviseringsdatum' />
<Value IncludeTimeValue='TRUE' Type='DateTime'><Today /></Value>
</Eq>
</Where>
</Query>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='Aviseringsdatum' />
<FieldRef Name='Avtalsansvarig' />
</ViewFields>
</View>")
$ListItems = Get-PnPListItem -List "Avtal" -Query $camlQuery
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.