$(document).ready(function () {

var itemProperties = { ‘Name’: ’19’ };
updateListItem(_spPageContextInfo.webAbsoluteUrl, ‘KVI’, ‘__bg40002300’, itemProperties, printInfo, logError);
function printInfo() {
console.log(‘Item has been created’);
}
function logError(error) {
console.log(JSON.stringify(error));
}
});

function updateJson(endpointUri, payload, success, error) {
$.ajax({
url: endpointUri,
type: “POST”,
data: JSON.stringify(payload),
contentType: “application/json;odata=verbose”,
headers: {
“Accept”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
“X-HTTP-Method”: “MERGE”,
“If-Match”: “*”
},
success: success,
error: error
});
}

function getItemTypeForListName(name) {
return “SP.Data.” + name.charAt(0).toUpperCase() + name.slice(1) + “ListItem”;
}

function updateListItem(webUrl, listTitle, listItemId, itemProperties, success, failure) {
var listItemUri = webUrl + “/_api/web/lists/getbytitle(‘” + listTitle + “‘)/items(” + listItemId + “)”;
var itemPayload = {
‘__metadata’: { ‘type’: getItemTypeForListName(listTitle) }
};
for (var prop in itemProperties) {
itemPayload[prop] = itemProperties[prop];
}
updateJson(listItemUri, itemPayload, success, failure);
}

Advertisements