window.dialog = function (ID) {
if (confirm("Press OK to delete plan with ID: " + ID + "") == true) { //Display the text with the ID of the item for the row you clicked the trash can for and if the user clicks "OK" then it runs the Erase function below with the ID just seen
Erase(ID);
} else {}
}
window.Erase = function (ID) { //REST API call to delete an item from the list based on the ID
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Title of List/Library')/items(" + ID + ")", //Returns only the items with the ID passed by the dialog function above
type: "POST",
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE", //Specifies to delete the item from the list
"If-Match": "*"
},
success: function (data) {
alert("Item deleted successfully!"); //Prompts user the item has been deleted successfully
},
error: function (err) {
alert("Error while deleting item: " + JSON.stringify(err)); //Prompts the user there was an error when trying to delete the item
}
});
location.reload(true); //Then force the page to reload to get rid of the newly deleted item from the table
}
To run this script make a hyperlink to 'javascript:dialog(' + ID(Could be from another SP API call) + ')';
This can be used to delete an item from a list based on the given ID of an item. It first prompts the user to ask the user if he/she is sure he/she wants to delete the item with the given ID.
This can be used to delete an item from a list based on the given ID of an item. It first prompts the user to ask the user if he/she is sure he/she wants to delete the item with the given ID.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.