Question

How to filter items between two dates with camel query?

For example, I want to retrieve items created between 2019-9-1 and 2019-10-5.

Was it helpful?

Solution

Date and time in sharepoint is ISO 8601 format. You could try the below script:

$spweb = get-spweb http://sp/sites/test/ 
$splist = $spweb.Lists.TryGetList("TestList")
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Query = 
   "   <Where>
      <And>
         <Geq>
            <FieldRef Name='Created' />
            <Value Type='DateTime'>2019-09-01T12:00:00Z</Value>
         </Geq>
         <Leq>
            <FieldRef Name='Created' />
            <Value Type='DateTime'>2019-10-05T12:00:00Z</Value>
         </Leq>
      </And>
   </Where>"; 
$items = $splist.GetItems($query)

OTHER TIPS

Try using something like below:

<Query>
  <Where>
    <And>
      <Geq>
        <FieldRef Name="Created" />
        <Value IncludeTimeValue="FALSE" Type="DateTime">2019-09-01</Value>
      </Geq>
      <Leq>
        <FieldRef Name="Created" />
        <Value IncludeTimeValue="FALSE" Type="DateTime">2019-10-5</Value>
      </Leq>
    </And>
  </Where>
</Query>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top