Delete ListItem in SharePoint using JSOM

The example in this topic show how to use JSOM to Delete ListItem in SharePoint

  • var oListItems, clientContext;
  •  
  • function DeleteListItem() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     // Specify list title here
  •     var oList = oWeb.get_lists().getByTitle("List Name");
  •  
  •     // Get Item using CAML Query
  •     var camlQuery = new SP.CamlQuery();
  •  
  •     camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Text\'>b</Value></Eq></Where></Query></View>');
  •     oListItems = oList.getItems(camlQuery);
  •  
  •     // Load list items to Client Context
  •     // "Include" will get only "Title" and "Id" properties, rather than fetching all the properties(This avoids unnecessary data transfer between client and server)
  •     clientContext.load(oListItems, 'Include(Title, Id)');
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •  
  •     // Get list item count of the items to be deleted
  •     var itemCount = oListItems.get_count();
  •  
  •     for (var i = 0; i <= itemCount - 1; i++) {
  •         var oListItem = oListItems.itemAt(i);
  •         oListItem.deleteObject();
  •     }
  •  
  •     //Execute the batch
  •     clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }
  •  
  • function FinalQuerySuccess(sender, args) {
  •     console.log("success");
  • }
  •  
  • function FinalQueryFailure(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013

We value your feedback:

Page URL:

Name:

 

Email:

 
 

Feedback:

 

© 2019 Code SharePoint