1

I want to delete a list element using CAML Query. The element has the value 55 in the column CorrespondingListID.

This is the function I use:

deleteFromHistory: function(){
  console.log("deleteFromHistory called!")
  let siteUrl = 'https://basketballulm1.sharepoint.com/sites/Playercard/';
  let clientContext = new SP.ClientContext(siteUrl);
  let oList = clientContext.get_web().get_lists().getByTitle('History');

  let camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'CorrespondingListID\'/>' +
          '<Value Type=\'Text\'>55</Value></Eq></Where></Query></View>');

  var collListItem = oList.getItems(camlQuery);
  clientContext.load(collListItem);
  console.log("collListItem: " + collListItem);

  var oListItem = collListItem.itemAt(1);
  console.log("oListItem: " + oListItem);
  oListItem.deleteObject();
  console.log("itemAt(1");

  //Execute the query
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded2), Function.createDelegate(this, this.onQueryFailed2));
},

The problem I have is that it tells me that deleteObject is undefined:

enter image description here

I've tried for hours, but I can't figure out what the problem is.

var oListItem = collListItem.itemAt(0);

doesn't exist either, so maybe there's something wrong with the CAML query?

Can someone tell me what I did wrong?

Thanks in advance!

2

colListItem will be empty until you execute the query at the end of your function. You need to execute it before you can delete the item(s), and then once again when the delete commands have been placed.

Check following code, I included comments on the bits I modified:

deleteFromHistory: function(){
    console.log("deleteFromHistory called!")
    let siteUrl = 'https://basketballulm1.sharepoint.com/sites/Playercard/';
    let clientContext = new SP.ClientContext(siteUrl);
    let oList = clientContext.get_web().get_lists().getByTitle('History');

    let camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'CorrespondingListID\'/>' +
            '<Value Type=\'Text\'>55</Value></Eq></Where></Query></View>');

    var collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    console.log("collListItem: " + collListItem);

    // Modified: 
    // First we much execute the query to retrieve the items
    clientContext.executeQueryAsync(()=> {
        // Now colListItem is a collection, we must iterate through it
        var listItemEnumerator = collListItem.getEnumerator();

        while (listItemEnumerator.moveNext()) {
            var item = listItemEnumerator.get_current();
            // if we try item.deleteObject() the collection will be modifed and it will throw an exception
            oList.getItemById(item.get_id()).deleteObject();
        }
        // Finally execute the delete statements
        clientContext.executeQueryAsync(
            () => console.log('Item(s) deleted')), 
            () => console.log('Failed to delete item(s)');
    }, 
    ()=>console.log('Failed to retrieve items'));
}
1
  • Thanks a lot man!! You made my day!!! Apr 2 '20 at 19:45
1

In your camel query in the set_viewxml there is a plus sign (+), remove that and try again. The best recommended way is - execute that query using camel query builder, then just copy paste the query inside set_viewxml.

You can download the camel query builder tool from here :

https://www.u2u.be/software

Refer to the below article how we can use :

https://www.c-sharpcorner.com/UploadFile/5b1f04/caml-query-builder-for-sharepoint-2013-on-premises/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.