Skip to content

Instantly share code, notes, and snippets.

@kparms
Last active June 14, 2016 17:37
Show Gist options
  • Save kparms/4b07a11182036fb3c1094f23621c3f43 to your computer and use it in GitHub Desktop.
Save kparms/4b07a11182036fb3c1094f23621c3f43 to your computer and use it in GitHub Desktop.
Update List Items in SharePoint 2013 REST
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);
}
function deleteJson(endpointUri,success, error) {
$.ajax({
url: endpointUri,
type: "POST",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"If-Match": "*"
},
success: success,
error: error
});
}
function deleteListItem(webUrl,listTitle,listItemId,success,failure) {
var listItemUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
deleteJson(listItemUri,success,failure);
}
var itemProperties = {'Title':'John Doe'};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',1,itemProperties,printInfo,logError);
deleteListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',1,printInfo,logError);
function printInfo()
{
console.log('Item has been created');
}
function logError(error){
console.log(JSON.stringify(error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment