It is much easier to create or update a list item in another site collection using JSOM. We can easily do it as follows:
I recommend using this method instead of the REST API to create a list item in another site collection.
var listName;
var item;
var itemType;
var otherClientContext = new SP.ClientContext("other site url");
var oList = otherClientContext.get_web().get_lists().getByTitle(listName);
otherClientContext.load(oList);
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', "SharePoint");
oListItem.update();
own1ClientContext.load(oListItem);
own1ClientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded() {
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
If we try to create a list item in another site collection using REST API call such as
$.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": $("#__REQUESTDIGEST").val()
},
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
we get an error
The security validation for this page is invalid and might be corrupted. Please use your web browser’s Back button to try your operation again.
The problem is we are trying to use the form digest $(“#__REQUESTDIGEST”).val() which will give the form digest for the current site collection.
We need to get the other site collection form digest value and provide it to the X-RequestDigest in the headers. In our case, we get this value by making an empty POST request to the
http:///_api/contextinfo and then use the d.GetContextWebInformation.FormDigestValue as the form digest value making the call.
Also, we need to specify method: “POST” instead of type: “POST” for the REST call to create the item.
We can construct the data for the item to be created as:
var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": { "type": itemType },
"Title": "updated title"
};
So, updated code to create a list item in another site collection is:
$.ajax({
url: otherSiteUrl + "/_api/contextinfo",
type: "POST",
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (contextData) {
$.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": $("#__REQUESTDIGEST").val(contextData.d.GetContextWebInformation.FormDigestValue)
},
success: function (data) {
alert('success');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
});
The post Create a list item in another site collection appeared first on SharePoint Guide.