When we have to implement Delete operation using REST API, we normally use the straightforward method to delete an item or a document. For example, to delete an item from a List, we use the following code.
To delete a document from a Library, we use the below code.
However, have you observed when a user deletes an item or a document from SharePoint UI, the deleted item is sent to Recycle Bin. So that, the user will get an option to restore the deleted item. But if we use the above approach, the deleted item will be permanently deleted and user will not get an option to restore.
So how do we mimic the out-of-the-box behavior through program?
SharePoint provides another endpoint called Recycle, with which we can send the deleted item to Recycle bin. Let's have a look at this method.
The code to recycle an item from a List:
The code to recycle a document:
You can also recycle item attachment and in this case you need to use another method RecycleObject, as shown below.
However do note that this method is not available in SharePoint 2013 on-prem environment. The below code works in SharePoint Online.
As shown above, you can use Recycle method to allow users to restore items in case they are deleted accidentally. I've not verified if RecycleObject is available in SharePoint 2016 on-prem. If I get a chance to validate, will update my findings here.
$.ajax({
type: "POST",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CustomList')/items(2)",
headers: {
"accept": "application/json; odata=verbose",
"X-HTTP-METHOD": "DELETE",
"IF-MATCH": "*",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert("Item is deleted!");
},
error: function(error){
alert("Error");
console.log(error);
}
});
To delete a document from a Library, we use the below code.
$.ajax({
type: "POST",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('/sites/pub/Documents/DocIcon.png')",
headers: {
"accept": "application/json; odata=verbose",
"X-HTTP-METHOD": "DELETE",
"IF-MATCH": "*",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert("Document is deleted!");
},
error: function(error){
alert("Error");
console.log(error);
}
});
However, have you observed when a user deletes an item or a document from SharePoint UI, the deleted item is sent to Recycle Bin. So that, the user will get an option to restore the deleted item. But if we use the above approach, the deleted item will be permanently deleted and user will not get an option to restore.
So how do we mimic the out-of-the-box behavior through program?
SharePoint provides another endpoint called Recycle, with which we can send the deleted item to Recycle bin. Let's have a look at this method.
The code to recycle an item from a List:
$.ajax({
type: "POST",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CustomList')/items(3)/recycle()",
headers: {
"accept": "application/json; odata=verbose",
"IF-MATCH": "*",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert("Item is recycled!");
},
error: function(error){
alert("Error");
console.log(error);
}
});
The code to recycle a document:
$.ajax({
type: "POST",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('/sites/pub/Documents/DocIcon.png')/recycle()",
headers: {
"accept": "application/json; odata=verbose",
"IF-MATCH": "*",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert("Document is recycled!");
},
error: function(error){
alert("Error");
console.log(error);
}
});
You can also recycle item attachment and in this case you need to use another method RecycleObject, as shown below.
However do note that this method is not available in SharePoint 2013 on-prem environment. The below code works in SharePoint Online.
$.ajax({
method: "DELETE",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CustomList')/getItemById(1)/AttachmentFiles/getByFileName('TestDoc.txt')/RecycleObject",
headers:
{"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert("File is deleted successfully");
},
error: function(error){
alert("Error in deleting attachment");
console.log(error);
}
});
As shown above, you can use Recycle method to allow users to restore items in case they are deleted accidentally. I've not verified if RecycleObject is available in SharePoint 2016 on-prem. If I get a chance to validate, will update my findings here.
Comments
Post a Comment