Sharepoint:如何更新使用 Sharepoint RestAPI 查找多个值的 ListItem 字段?
Sharepoint: How to update a ListItem field which is Lookup multiple values using Sharepoint RestAPI?
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."}}}"
查找字段内部名称正确。
错误:
这里有人可以解决我的问题吗?非常感谢。
您尝试 post 数据时似乎出现了一些问题。它可能与 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);
}
});
}
终于找到错误了。字段名是正确的"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);
}
});
}
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."}}}"
查找字段内部名称正确。
错误:
这里有人可以解决我的问题吗?非常感谢。
您尝试 post 数据时似乎出现了一些问题。它可能与 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);
}
});
}
终于找到错误了。字段名是正确的"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);
}
});
}