<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var otherSiteUrl = "http://sp/sites/jerry";
var listName = "jerrylist";
var itemType = GetItemTypeForListName(listName);
var item = {
"__metadata": {
"type": itemType
},
"Title": "updated again"
};
// Get form digest value of the other site collection
$.ajax({
url: otherSiteUrl + "/_api/contextinfo",
type: "POST", //While trying to get Context info for user POST is used as type instead of method because body is empty while //request is sent
headers: {
"Accept": "application/json;odata=verbose"
},
success: function(contextData) {
console.log(contextData.d.GetContextWebInformation.FormDigestValue);
$.ajax({
url: otherSiteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(1)?@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,
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE"
},
success: function(data) {
alert('success');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error');
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error');
}
});
});
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}
</script>
Comments
Post a Comment