Home > Mobile >  How to add 1 (increment) to the current value of a SharePoint list number field using the rest API
How to add 1 (increment) to the current value of a SharePoint list number field using the rest API

Time:12-23

I am implementing a custom solution on SharePoint Online, and, I want to add 1 (increment) to the current value of a specific field number (TotalViews) using rest API.

Here is my code:

function UpdateViewsinHow(param)
 {
        var data = {
            __metadata: { 'type': 'SP.Data.HowListListItem' },

           TotalViews : TotalViews 1  // here how to add 1 to current Totalviews
           
        };
        $.ajax({
            url: siteUrl   "/_api/web/lists/getbytitle('HowList')/items(" param ")",
            method: "PATCH",
            data: JSON.stringify(data),
            headers: {
                "Accept": "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
               "IF-MATCH": "*",
               "X-HTTPS-Method": "MERGE"
            },
            success: function (data) {
                // confirm
            },
            error: function (error) {
                alert("Error: "   JSON.stringify(error));
            }
        });
}

Any advice to achieve this step?

CodePudding user response:

You have to first retrieve the current value of number column to be able to add 1 and increment it, before you update it.

To do that, you can nest what you currently have inside the success function of a request to get the current value of TotalViews:

function UpdateViewsinHow(param) {
    // first get the current value of TotalViews
    $.ajax({
        url: siteUrl   "/_api/web/lists/getbytitle('HowList')/items("   param   ")?$select=TotalViews",
        method: "GET",
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (response) {
            var update = {
                __metadata: { 'type': 'SP.Data.HowListListItem' },
                
                // you may have to adjust this to get to the value of TotalViews
                // in the response, but it should be something close to this:
                TotalViews: response.d.TotalViews   1

            };
            $.ajax({
                url: siteUrl   "/_api/web/lists/getbytitle('HowList')/items("   param   ")",
                method: "PATCH",
                data: JSON.stringify(update),
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "IF-MATCH": "*",
                    "X-HTTPS-Method": "MERGE"
                },
                success: function (data) {
                    // confirm
                },
                error: function (error) {
                    alert("Error: "   JSON.stringify(error));
                }
            });
        },
        error: function (error) {
            alert("Error: "   JSON.stringify(error));
        }
    });
}
  • Related