Sharepoint:如何使用Sharepoint RestAPI更新Lookup多个值的ListItem字段?

时间:2017-09-21 02:35:22

标签: jquery rest sharepoint sharepoint-2013

Sharepoint:如何使用Sharepoint RestAPI更新Lookup多个值的ListItem字段?

我想更新一个ListItem字段,这个字段类型是Lookup多值。我编写了样本测试代码,但它不起作用。

这是我的代码:

 function UpdateItemLookup() {
    // Getting our list items
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('testlist')/Items(1)",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        cache: true,
        async: false,
        success: function (data) {
            Update(data);
        },
        error: function (data) {
            console.log(data);
        }
    });
}

function Update(result) {
    $.ajax({          
        url: result.d.__metadata.uri,
        type: "POST",
        contentType: "application/json;odata=verbose",
        body: JSON.stringify({
            "__metadata": { type: "SP.Data.Test_x0020_listListItem" },
            Title: "bbbb",
            lookup1: [1, 3] //the lookup field needs to be updated to "1" and "3" 
        }),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-type": "application/json;odata=verbose",
            "X-HTTP-Method": "MERGE",
            "If-Match": result.d.__metadata.etag
        },
        success: function (result) {
            console.log(result);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

我收到此错误:

"{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected."}}}"

lookupfield内部名称是正确的。 enter image description here

错误: enter image description here

这里的任何人都可以解决我的问题吗?非常感谢。

2 个答案:

答案 0 :(得分:1)

当您尝试发布数据时,似乎存在一些问题。它可能与json stringify有关。

请尝试使用Update方法的以下更新代码:

function Update(result) {

    var itemMetadata = {
        "__metadata": { "type": "SP.Data.Test_x0020_listListItem" }, 
        "Title": "bbbb",
        "lookup1": {"results": [1,3] }
    }; 

    $.ajax({          
        url: result.d.__metadata.uri,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemMetadata),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),            
            "X-HTTP-Method": "MERGE",
            "If-Match": "*"
        },
        success: function (result) {
            console.log(result);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

答案 1 :(得分:1)

我终于找到了错误。字段名称是正确的" lookup1",但是当我更新此字段的值时,我需要添加后缀" Id"在字段名称之后。我的情况应该是" lookup1Id'它有效。

 function Update(result) {
    var item = $.extend({
        "__metadata": { "type": "SP.Data.Test_x0020_listListItem" }
    }, {
        Title: "test item aaaa",
        lookup1Id: { "results": [1, 3] }, //here is the problem, it should be "lookup1Id"
    });

    $.ajax({            
        url: result.d.__metadata.uri,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "MERGE",
            "If-Match": result.d.__metadata.etag
        },
        success: function (result) {
            console.log(result);
        },
        error: function (error) {
            console.log(error);
        }
    });
}