1

My end goal is to display items from SharePoint 2013 list that is more than 60 days from today and status is in process. This is my first time using CAML Query in JavaScript. I am not able to have the filtered list display. There's no error in developer console. What am I missing?

$(document).ready(
            function() {

                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() {

                    var context = new SP.ClientContext.get_current();
                    var list = context.get_web().get_lists().getByTitle("Tasks List");

                    var query = new SP.CamlQuery();
                    
                    query.set_viewXml("<View>" 
                        +"<Query>" 
                           +" <Where>"
                     +"<And>"
                     +"<Eq>"
                     +"<FieldRef Name='Status' />"
                     +"<Value Type='Choice'>In Process</Value>"
                     +" </Eq>"
                     +" <And>"
                     +"<Leq>"
                     +"<FieldRef Name='Start_x0020_Date' />"
                     +"<Value Type='DateTime'>"
                     + "<Today OffsetDays= '-60'>"
                     +"</Value>"
                     +" </Leq>"
                     +"</And>"
                     +"</And>"
                     +"</Where>"
                     +"</Query>"
                     +"</View>");

                    listItem = list.getItems(query);
                    context.load(list Item);
                    context.executeQueryAsync(console.log(view.get_viewQuery()))

                }); // SP.SOD 
            }) //doc
</script>

2 Answers 2

0

The camel query should be this:

   <Where>
      <And>
         <Eq>
            <FieldRef Name='Status' />
            <Value Type='Choice'>In Progress</Value>
         </Eq>
         <Leq>
            <FieldRef Name='StartDate' />
            <Value Type='DateTime'>
               <Today Offset='-60' />
            </Value>
         </Leq>
      </And>
   </Where>

Below is my demo code:

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script><script type="text/javascript" src="/_layouts/15/sp.js"></script><script>
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getListItems);
    var items;
    function getListItems(){
        var clientContext = SP.ClientContext.get_current();
        var web = clientContext.get_web();  
        var list= web.get_lists().getByTitle("Tasks List");
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Status' /><Value Type='Choice'>In Progress</Value></Eq><Leq><FieldRef Name='StartDate' /><Value Type='DateTime'><Today Offset='-60' /></Value></Leq></And></Where></Query></View>");

        items =list.getItems(query);
        clientContext.load(items);
        clientContext.executeQueryAsync(onsuccess, onfailed);
    }
    function onsuccess() {
 
    // Iterate through Enumerator
    var oEnumerator = items.getEnumerator();
 
    while (oEnumerator.moveNext()) {
        // get_item("ColumnName")
        var item = oEnumerator.get_current().get_item('Title');
        console.log("Title: " + item);
    }
}
 
function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>
0

If you want to get the list items where Status is equal to "In Progress" and Start Date is greater than today + 60 (correct me if I am wrong) then you should use your query like below:

<View>
    <Query>
        <Where>
            <And>
                <Eq>
                    <FieldRef Name='Status' />
                    <Value Type='Choice'>In Process</Value>
                </Eq>
                <Geq>
                    <FieldRef Name='Start_x0020_Date' />
                    <Value Type='DateTime'>
                        <Today OffsetDays='60' />
                    </Value>
                </Geq>
            </And>
        </Where>
    </Query>
</View>
4
  • Thank you for your help, but I am still not able to get the filtered list.
    – Madeline
    Commented Aug 10, 2020 at 20:40
  • Can you please mention all the filter conditions you want to apply in CAML? The date condition you mentioned is bit confusing. Can you please rephrase it again? Commented Aug 11, 2020 at 3:18
  • I wanted to filter the status column containing "In-Process" that the "Start Date" goes back 60 days from today. i.e. start date would be June 10th 2020 and earlier. thanks
    – Madeline
    Commented Aug 11, 2020 at 3:53
  • In that case use <Today OffsetDays='-60' /> in CAML query. Commented Aug 11, 2020 at 5:41

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.