0

I am attempting to get ALL folders that contain a value for the ProjectNumber field and were modified within the last five days. This query only works when I remove the modified condition. Otherwise it throws a SharePoint REST API exception. How can I query on the modified field?

<View Scope='RecursiveAll'>
<ViewFields>
    <FieldRef Name='ProjectNumber' />
    <FieldRef Name='FileDirRef' />
</ViewFields>
<Query>
  <Where>
    <And>
        <IsNotNull>
            <FieldRef Name='ProjectNumber' />   
        </IsNotNull>
        <Eq>
            <FieldRef Name='FSObjType' />
            <Value Type='Integer'>1</Value>
        </Eq>
        <Geq>
            <FieldRef Name='Modified' />
            <Today OffsetDays='-5'/>
        </Geq>
    </And>
  </Where>
</Query>
<OrderBy Override='TRUE'><FieldRef Name= 'ProjectNumber' Ascending= 'FALSE' /></OrderBy>
</View>

1 Answer 1

0

You are missing the <Value /> element around the <Today /> element. Try the below:

<View Scope='RecursiveAll'>
<ViewFields>
    <FieldRef Name='ProjectNumber' />
    <FieldRef Name='FileDirRef' />
</ViewFields>
<Query>
  <Where>
    <And>
        <IsNotNull>
            <FieldRef Name='ProjectNumber' />   
        </IsNotNull>
        <Eq>
            <FieldRef Name='FSObjType' />
            <Value Type='Integer'>1</Value>
        </Eq>
        <And>
        <Geq>
            <FieldRef Name='Modified' />
            <Value Type ='DateTime'><Today OffsetDays='-5'/></Value>
        </Geq>
        </And>
    </And>
  </Where>
</Query>
<OrderBy Override='TRUE'><FieldRef Name= 'ProjectNumber' Ascending= 'FALSE' /></OrderBy>
</View>

Also note that in CAML each AND/OR conditional aggregates can contain only a max of two filters. In order to have more than two you have to concatenate them with multiple AND/OR.

You could use some CAML Query Builder like U2U Caml Query Builder to test CAML queries out.

3
  • Thanks for that. I tried this exact query but get a REST Exception. :-/ It gives "code=-2147467259, Microsoft.SharePoint.SPException" and "value=Cannot complete this action. Please try again." when I add in that <Geq> block. Removing the block, the query will work.
    – Adam
    Commented Jul 24, 2023 at 14:23
  • See my updated response
    – Gaurav T
    Commented Jul 25, 2023 at 11:54
  • This also produced a REST exception. I think I will have to switch directions and query a different data source rather than using CAML querying with SharePoint since the query also only ever returns 27 results even without the where conditions and there are way more than 27 items.
    – Adam
    Commented Jul 25, 2023 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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