In this blog, I would like to share the code to delete the SharePoint list item using SharePoint Rest API.
Here is the detailed
documentation for the lists and their features.
Steps to delete the SharePoint list Item
Follow the below steps to delete an item.
Step 1
Create one JS file or you can use the Content Editor Web Part.
Step 2
Below is the API to get the particular list item using Item ID, you can browse this URL in the browser to check whether the API is working or not.
https://sharepoint.com/_api/web/lists/GetByTitle('samplelist')/items/getbyid(1)
Note
Replace your list name with “samplelist”.
Step 3
Refer the jQuery in your HTML file or CEWP.
Use the below code to delete the item from a SharePoint list.
- 'use strict';
- ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
-
- function initializePage() {
- var siteURL;
- var itemsArray = [];
-
- $(document).ready(function() {
- var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
- DeleteListItem();
- });
-
- function DeleteListItem() {
- siteURL = _spPageContextInfo.webAbsoluteUrl;
- console.log("from top nav - " + siteURL);
- var apiPath = siteURL + "/_api/lists/getbytitle(''samplelist'')/items/getbyid(1)";
- $.ajax({
- url: apiPath,
- type: "DELETE",
- headers: {
- Accept: "application/json;odata=verbose"
- },
- async: false,
- success: function(data) {
- alert("Item Deleted successfully");
- },
- eror: function(data) {
- console.log("An error occurred. Please try again.");
- }
- });
- }
- }
Summary
In this blog, we have explored how to delete a particular item from SharePoint list using REST API.