Domanda

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>
È stato utile?

Soluzione

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
        }
    });
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top