Pakkiya Nathan
Pakkiya Nathan

Reputation: 84

Sum of Sharepoint list items using Rest API

I want to get the total sum of one field from my sharepoint list using REST API ..am using the following code but my output is like 501001005045

I don't know Where am Do the Mistake

Here is my code

function GetSumApi(projectTitle) {
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('TaskList')/items?$select=TaskPercentage&$filter=Projects eq '" + projectTitle + "'",
    type: "GET",
    async: false,
    headers: {
        "Accept": "application/json;odata=verbose",
    },
    success: function (data) {
        sumfield = data.d.results;
        var sum = 0;
        for (var i = 0; i < sumfield.length; i++) {
            var sum = sum + sumfield[i].TaskPercentage;
        }
        console.log("Total sum of Tasks", sum);
    },
    error: function (error) {
        console.log(JSON.stringify(error));
    }
});

}

In the above code am using list name as TaskList and my field is taskpercentage and the project title is the project name from the user.

Upvotes: 1

Views: 784

Answers (1)

Lee
Lee

Reputation: 5493

Use parseInt to convert the value to int.

For example:

var sum = 0;
for (var i = 0; i < sumfield.length; i++) {
        var sum = sum + parseInt(sumfield[i].TaskPercentage);//parseInt(sumfield[i].TaskPercentage)/100
    }

Upvotes: 2

Related Questions