1

We have a list with over 1M items. The end goal is to filter the column InvDate based on date range.

The InvDate is an indexed single line of text column , However it throws the List View Threshold error while getting the item using RestAPI

Code

function FilterDate()
{
  //var initialUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ClaimListNew')/Items?$select=ID,*";
  
var sdate = $('#sdate').val();
var edate = $('#edate').val();


var myQuery= _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getbytitle('MyList')/items?$select=ID*";
myQuery+="&$filter=InvDate ge '" +sdate+ "' and InvDate le '" +edate+ "'";
myQuery+="&$top=4999";
myQuery+="&$orderby=ID desc";


// start the process
var getAllItemsRequest = GetAllItems(myQuery);

// wait for it all to complete
getAllItemsRequest.done(function (allItems) {

console.log(allItems);
   
  
    
}).fail(function (error) {
    console.warn("Error getting all items");
    console.warn(JSON.stringify(error));
});
}

Function to get all the list items

function GetAllItems(requestUrl) {
    var deferred = $.Deferred();
    $.ajax({
        url: requestUrl,
        method: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        }
    }).done(function (thisResult) {
        if (thisResult.d.__next) {
            // if the __next property is present,
            // that measn that there's more to get,
            // so don't start resolving things yet,
            // just make the next request
            var nextRequest = GetAllItems(thisResult.d.__next);
            
            // since GetAllItems returns a promise,
            // we have to wait for that promise to resolve
            // before resolving the promise we made 
            // at the beginning of this function
            nextRequest.done(function(nextResult) {
                // nextResult should be a plain array of items,
                // so we need to combine that with the array
                // of items we got in this call in thisResult.d.results
                var combinedResults = thisResult.d.results.concat(nextResult);
                
                // then resolve the combined results back up to the next level
                deferred.resolve(combinedResults);

            }).fail(function(error) {
                deferred.reject(error);
            });
        } else {
            // __next doesn't exist, so
            // there's nothing more to get, so
            // start resolving the nested promises
            deferred.resolve(thisResult.d.results);
        }
    }).fail(function (result) {
        deferred.reject(result);
    });
    return deferred.promise();
};

The above function has retrieved all 1 Million items without any filters as below

var myQuery= _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getbytitle('ClaimlistNew')/items?$select=ID,*"; enter image description here

When we use query with the below filter , we get the threshold error

var myQuery= _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getbytitle('MyList')/items?$select=ID*"; myQuery+="&$filter=InvDate ge '" +sdate+ "' and InvDate le '" +edate+ "'"; myQuery+="&$top=4999"; myQuery+="&$orderby=ID desc";

The end goal is to filter the InvDate column with the date range.

Would be grateful if anyone could let me know how this can be achieved.

1 Answer 1

1

As the column type is text so Greater than and Less than condition will not work so you should first convert the column type to Date or DateTime.

Then you need to check below.

  1. The date should be in proper format. you can pass ISOString format date
  2. & should be used as &

so the FilterDate function should look like below

function FilterDate()
{
  //var initialUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ClaimListNew')/Items?$select=ID,*";
  
var sdate = new Date($('#sdate').val()).toISOString();
var edate = new Date($('#edate').val()).toISOString();


var myQuery= _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getbytitle('MyList')/items?$select=ID*";
myQuery+="&$filter=(InvDate ge DateTime'"+sdate+"') and (InvDate le DateTime'"+edate+"'");
myQuery+="&$top=4999";
myQuery+="&$orderby=ID desc";


// start the process
var getAllItemsRequest = GetAllItems(myQuery);

// wait for it all to complete
getAllItemsRequest.done(function (allItems) {

console.log(allItems);
   
  
    
}).fail(function (error) {
    console.warn("Error getting all items");
    console.warn(JSON.stringify(error));
});
}
16
  • Thank you for replying Harminder. The date is in correct format. As suggested by you , would use &amp and check the behavior
    – Vivek
    Commented Jul 6, 2021 at 6:43
  • Harminder , this seems to work , However it is getting all the entries from the list and not filtering it based on date range. The InvDate field is an indexed single line of text. Any suggestions?
    – Vivek
    Commented Jul 6, 2021 at 7:07
  • @Vivek can you try the filter like (InvDate ge DateTime'sDate') and(InvDate le DateTime'edate')and top as last parameter. I have tried the same and getting correct results Commented Jul 6, 2021 at 8:05
  • Harminder would you mind editing your answer and elaborating it? I am getting the dates from jquery datepicker and passing it to query dynamically. Do you mean I need to parse the value received from jquery datepicker to DateTime's Date?
    – Vivek
    Commented Jul 6, 2021 at 8:11
  • 1
    Harminder your solution would only work if the column is of type datetime. In my scenario my column is single line of text. Please check this stackoverflow.com/questions/36897613/…
    – Vivek
    Commented Jul 6, 2021 at 12:02

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.