0

I built a CAML query using U2U CAML Query Builder and it works when I execute it in the builder, but when I go to JSOM in SharePoint, I get the entire list and none of the filters from the query work.

Query

<Query>
   <Where>
      <And>
         <Geq>
            <FieldRef Name='Created1' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>2014-10-29T10:07:07Z</Value>
         </Geq>
         <Leq>
            <FieldRef Name='Created1' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>2014-11-04T09:07:08Z</Value>
         </Leq>
      </And>
   </Where>
   <OrderBy>
      <FieldRef Name='Title' Ascending='True' />
   </OrderBy>
</Query>

Here is the code that isn't working. What am I doing wrong?

function retrieveListItemsInclude() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('Incidents');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<Query><Where><And><Geq><FieldRef Name=\'Created1\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>2014-10-29T10:07:07Z</Value></Geq><Leq><FieldRef Name=\'Created1\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>2014-11-04T09:07:08Z</Value></Leq></And></Where><OrderBy><FieldRef Name=\'Title\' Ascending=\'True\' /></OrderBy></Query>');
    window.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(Title, Created1, Severity)');

    clientContext.executeQueryAsync(Function.createDelegate(this, window.onQuerySucceeded), Function.createDelegate(this, window.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nTitle: ' + oListItem.get_item('Title') + 
            '\nCreated: ' + oListItem.get_item('Created1');
    }

    console.log(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

1 Answer 1

0

You need to enclose the query with <View> tag

camlQuery.set_viewXml('<View><Query><Where><And><Geq><FieldRef Name=\'Created1\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>2014-10-29T10:07:07Z</Value></Geq><Leq><FieldRef Name=\'Created1\' /><Value IncludeTimeValue=\'TRUE\' Type=\'DateTime\'>2014-11-04T09:07:08Z</Value></Leq></And></Where><OrderBy><FieldRef Name=\'Title\' Ascending=\'True\' /></OrderBy></Query></View>');
3
  • Please specify where this differs from the original code, for clarity Nov 7, 2014 at 20:16
  • 1
    @RobertLindgren updated answer. Nov 7, 2014 at 20:18
  • Thanks! That worked! I looked at that code for such a long time I missed that.
    – USR961
    Nov 10, 2014 at 15:32

Your Answer

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

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