代码之家  ›  专栏  ›  技术社区  ›  anhtv13

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

  •  0
  • anhtv13  · 技术社区  · 9 年前

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

    我想更新一个ListItem字段,该字段类型为Lookup multiple value。我编写了样本测试代码,但它不起作用。

    这是我的代码:

     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 internalname正确。 enter image description here

    错误: enter image description here

    这里有人能解决我的问题吗?非常感谢。

    2 回复  |  直到 6 年前
        1
  •  3
  •   anhtv13    9 年前

    我终于找到了错误。字段名正确为“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);
            }
        });
    }
    
        2
  •  1
  •   Gautam Sheth    9 年前

    当您试图发布数据时,似乎出现了一些问题。这可能与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);
            }
        });
    }