jquery 如何使用rest API向SharePoint列表编号字段的当前值加1(增量)

ut6juiuv  于 2022-12-22  发布在  jQuery
关注(0)|答案(1)|浏览(111)

我正在SharePoint Online上实现一个自定义解决方案,我想使用rest API将特定字段编号(TotalViews)的当前值加1(增量)。
下面是我的代码:

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));
            }
        });
}

对于实现这一步有什么建议吗?

mf98qq94

mf98qq941#

您必须首先检索number列的当前值,以便能够在更新它之前加1并递增它。
要做到这一点,可以将当前的内容嵌套在请求的success函数中,以获取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));
        }
    });
}

相关问题