1

I am retrieving items from a SharePoint list view. I want to further filter the CAML query to give the only items modified in last 24 hours. Here is my code

        function getListItems(webUrl,listTitle, queryText) 
 {
  var today = new Date();
  var viewXml = '<View><Query>' + queryText +'<Geq><FieldRef Name="Modified"/><Value Type="DateTime">today-1</Value></Geq></Query></View>';
  var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
  var queryPayload = {  
           'query' : {
                  '__metadata': { 'type': 'SP.CamlQuery' }, 
                  'ViewXml' : viewXml  
           }
};

 return executeJson(url,"POST",null,queryPayload);

}

2

You filter would need to be something like that:

<Geq><FieldRef Name="Modified"/><Value Type="DateTime"><Today Offset="-1" /></Value></Geq>

Edit: Sorry, my answer was solely targeting the filter by date issue. Depending on what you provide as queryText parameter, you will need to add an extra And operator, as pointed out by Damjan Tomic in the comments:

  • if it is an extra filter, like <Eq><FieldRef Name='Foo' /><Value Type='Text'>Bar</Value></Eq>, you will need to add the And or another boolean operator to combine the two filters together.
  • if it is an OrderBy or another Query child element, this is fine.

In both cases however, you are missing the Where element which is where filters should be defined.

So, if you pass your OrderBy as the queryText parameter you should have that :

var viewXml = '<View><Query>' + queryText +'<Where><Geq><FieldRef Name="Modified" /><Value Type="DateTime"><Today Offset="-1" /></Value></Geq></Where></Query></View>';

If you pass another filter, you will have to do that :

var viewXml = '<View><Query><Where><And>' + queryText +'<Geq><FieldRef Name="Modified" /><Value Type="DateTime"><Today Offset="-1" /></Value></Geq></And></Where></Query></View>';

  • Besides this the query should be changed to Include <And> tag. The whole line should look like this: var viewXml = '<View><Query><And>' + queryText +'<Geq><FieldRef Name="Modified" /><Value Type="DateTime"><Today OffsetDays="-1" /></Value></Geq></And></Query></View>'; – Damjan Tomic Feb 9 '17 at 17:19
  • This is the Final CAML "<View><Query><And><OrderBy><FieldRef Name="Modified" Ascending="FALSE" /></OrderBy><Geq><FieldRef Name="Modified" /><Value Type="DateTime"><Today OffsetDays="-1" /></Value></Geq></And></Query></View>" but it returns all the items i get items from 2014. I am looking for items that was modified in 2/8/2017 or earlier. – user388969 Feb 9 '17 at 17:35
  • 1
    Thanks to @DamjanTomic for pointing out a potential issue with combining filters through the use of the queryText parameter. – Reego Feb 13 '17 at 10:15

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.