在 Sharepoint 2013 中更新另一个站点 collection 中的项目
Update item in another site collection in sharepoint 2013
我需要根据这个更新另一个站点中的项目 collection article 我的代码是这样
<script type="text/javascript">
$(document).ready(function () {
var otherSiteUrl = "<sitecollectionurl>";
var listName = "TestList";
var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": {
"type": itemType
},
"Title": "updated title"
};
$.ajax({
url: otherSiteUrl + "/_api/contextinfo",
type: "POST",
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (contextData) {
alert(contextData.d.GetContextWebInformation.FormDigestValue);
$.ajax({
url: otherSiteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?@target='" + otherSiteUrl + "'",
method: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
async: false,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue
},
success: function (data) {
alert('success');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
});
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
</script>
我用过
"X-RequestDigest":
contextData.d.GetContextWebInformation.FormDigestValue
然后我陈哥ajaxheader喜欢
"X-RequestDigest":
$("#__REQUESTDIGEST").val(contextData.d.GetContextWebInformation.FormDigestValue)
两个 RequestDigest
我都收到了这个错误:
Invalid JSON. A token was not recognized in the JSON content
after that I chenge ajax header as
如何才能在另一个站点 collection 和 api 中成功更新项目?
最终,我可以在另一个站点中编辑项目 collection.I 只需稍微更改我的代码即可。
首先,最重要的部分是库全名不包含 List
我用这个 url 来找出库全名。
[Site url]/_api/web/lists/GetByTitle('List Name')/ListItemEntityTypeFullName
然后我将元数据更改为
"__metadata": { "type": 'SP.Data.DocumentsItem' },
秒我加
"X-HTTP-Method": "MERGE" , "If-Match": "*"
到 header 赞:
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue,
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
我需要根据这个更新另一个站点中的项目 collection article 我的代码是这样
<script type="text/javascript">
$(document).ready(function () {
var otherSiteUrl = "<sitecollectionurl>";
var listName = "TestList";
var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": {
"type": itemType
},
"Title": "updated title"
};
$.ajax({
url: otherSiteUrl + "/_api/contextinfo",
type: "POST",
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (contextData) {
alert(contextData.d.GetContextWebInformation.FormDigestValue);
$.ajax({
url: otherSiteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?@target='" + otherSiteUrl + "'",
method: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
async: false,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue
},
success: function (data) {
alert('success');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
});
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
</script>
我用过
"X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue
然后我陈哥ajaxheader喜欢
"X-RequestDigest": $("#__REQUESTDIGEST").val(contextData.d.GetContextWebInformation.FormDigestValue)
两个 RequestDigest
我都收到了这个错误:
Invalid JSON. A token was not recognized in the JSON content after that I chenge ajax header as
如何才能在另一个站点 collection 和 api 中成功更新项目?
最终,我可以在另一个站点中编辑项目 collection.I 只需稍微更改我的代码即可。
首先,最重要的部分是库全名不包含 List
我用这个 url 来找出库全名。
[Site url]/_api/web/lists/GetByTitle('List Name')/ListItemEntityTypeFullName
然后我将元数据更改为
"__metadata": { "type": 'SP.Data.DocumentsItem' },
秒我加
"X-HTTP-Method": "MERGE" , "If-Match": "*"
到 header 赞:
headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue, "X-HTTP-Method": "MERGE", "If-Match": "*" },