0

LEE_MSFT corrected my code below. Code works well but the only problem i have that total item count is 319. But sum of each count is 21(Active),77(Closed),2(Partial),0(Special). Can anyone help me why totals are not matching. Couldn't figure out what's going wrong. Also File Status field is a choice column so spelling mistake would not be the case for the wrong totals

  <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
     <script type="text/javascript">
        var siteurl = _spPageContextInfo.webAbsoluteUrl;
                $.ajax({
                           url: siteurl + "/_api/web/lists/getbytitle('Volunteer%20Master%20List')/items",
                           method: "GET",
                           headers: { "Accept": "application/json; odata=verbose" },
                           success: function (data) {
                                if (data.d.results.length > 0 ) {
                                        var sum =0;
                                        var sum1 =0;
                                        var sum2 =0;
                                        var sum3 =0;
                                        $.each(data.d.results, function(index,item){
                                            if(item.File_x0020_Status  === "Active")
                                              sum++;
                                            if(item.File_x0020_Status === "Closed")
                                              sum1++;
                                            if(item.File_x0020_Status === "Partial")
                                              sum2++;
                                            if(item.File_x0020_Status === "Special Project")
                                              sum3++;
                                        }); 
                                        console.log(sum);
                                        console.log(sum1);
                                        console.log(sum2);
                                        console.log(sum3);
                                }
                            document.getElementById("myelement").innerHTML=sum;
    document.getElementById("myelement1").innerHTML=sum1;
    document.getElementById("myelement2").innerHTML=sum2;
    document.getElementById("myelement3").innerHTML=sum3;
                          },
                          error: function (data) {
                              alert("Error: "+ data);
                         }
                  });
        </script>

        <div id="myelement"></div>
    <div id="myelement1"></div>
    <div id="myelement2"></div>
    <div id="myelement3"></div>
  • 1
    Do you have items with empty value for File_x0020_Status field? – Damjan Tomic Nov 26 at 20:24
  • No this is a mandatory field. All 319 items have a File status – Mani Nov 26 at 23:28
2

By default the rest api returns only 100 items. We can add $top=5000 in rest api to solve your issue.

/_api/web/lists/getbytitle('Volunteer%20Master%20List')/items?$top=5000

If you have more than 5000 items, we can use the code like below.

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Volunteer%20Master%20List')/items?$top=1000";
var response = response || [];  // this variable is used for storing list items
function GetListItems(){
    $.ajax({
        url: url,  
        method: "GET",  
        headers: {  
            "Accept": "application/json; odata=verbose"  
        },
        success: function(data){
            response = response.concat(data.d.results);
            if (data.d.__next) {
                url = data.d.__next;
                GetListItems();
            }
        },
        error: function(error){
               // error handler code goes here
        }
    });
}
  • Thank you so much adding this items?$top=5000 in the code Worked for me. You are amazing. Just to make sure the code you wrote would give me total item count without filter. – Mani Nov 27 at 18:22

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.