Skip to main content
added 885 characters in body
Source Link
SP 2022
  • 4.1k
  • 3
  • 18
  • 41

You can use the camel query to get the list item based on your column value which will load only those list items which meetsmeet that criteria,then then do the delete operation as usual way.

To filter SP.ListItem's on mulitplemultiple conditions, there are no single methods that take the arguments as multiple filter fields. Instead, you'll have to resort to using a CAML query to specify the list items you want, as below.

var clientContext = new SP.ClientContext( siteURL );
spList = clientContext.get_web().get_lists().getByTitle( myListName );

//Create a CAML-query with your filter conditions
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + 
    '<Value Type=\'Number\'>30</Value></Eq>
    <Eq><FieldRef Name=\'Country\'/>' + 
    '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>');

//The query will return a collection of items matching your conditions
this.collListItem = spList.getItems(camlQuery);

clientContext.load(collListItem);

//Execute the query
clientContext.executeQueryAsync(function () {

    var itemCount = collListItem.get_count();
    //For each list item in the collection, mark it to be deleted
    for (var i = itemCount - 1; i >= 0; i--) {
        var oListItem = collListItem.itemAt(i);
        oListItem.deleteObject();
    };

    //Execute the delete operation
    clientContext.executeQueryAsync(deleteSucceeded, deleteFailed);
}, fail_handler);  

Note:

In your case, the set_viewXml should be something like below, if you get syntax error use camel query builder tool.

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'CorrespondingListID\'/>' + 
    '<Value Type=\'Text\'>somevaluefrom CorrespondingListID</Value></Eq></Where></Query></View>');

You can use the camel query to get the list item based on your column value which will load only those list items which meets that criteria,then do the delete operation as usual way.

To filter SP.ListItem's on mulitple conditions, there are no single methods that take the arguments as multiple filter fields. Instead, you'll have to resort to using a CAML query to specify the list items you want, as below.

var clientContext = new SP.ClientContext( siteURL );
spList = clientContext.get_web().get_lists().getByTitle( myListName );

//Create a CAML-query with your filter conditions
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + 
    '<Value Type=\'Number\'>30</Value></Eq>
    <Eq><FieldRef Name=\'Country\'/>' + 
    '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>');

//The query will return a collection of items matching your conditions
this.collListItem = spList.getItems(camlQuery);

clientContext.load(collListItem);

//Execute the query
clientContext.executeQueryAsync(function () {

    var itemCount = collListItem.get_count();
    //For each list item in the collection, mark it to be deleted
    for (var i = itemCount - 1; i >= 0; i--) {
        var oListItem = collListItem.itemAt(i);
        oListItem.deleteObject();
    };

    //Execute the delete operation
    clientContext.executeQueryAsync(deleteSucceeded, deleteFailed);
}, fail_handler);  

You can use the camel query to get the list item based on your column value which will load only those list items which meet that criteria, then do the delete operation as usual way.

To filter SP.ListItem's on multiple conditions, there are no single methods that take the arguments as multiple filter fields. Instead, you'll have to resort to using a CAML query to specify the list items you want, as below.

var clientContext = new SP.ClientContext( siteURL );
spList = clientContext.get_web().get_lists().getByTitle( myListName );

//Create a CAML-query with your filter conditions
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + 
    '<Value Type=\'Number\'>30</Value></Eq>
    <Eq><FieldRef Name=\'Country\'/>' + 
    '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>');

//The query will return a collection of items matching your conditions
this.collListItem = spList.getItems(camlQuery);

clientContext.load(collListItem);

//Execute the query
clientContext.executeQueryAsync(function () {

    var itemCount = collListItem.get_count();
    //For each list item in the collection, mark it to be deleted
    for (var i = itemCount - 1; i >= 0; i--) {
        var oListItem = collListItem.itemAt(i);
        oListItem.deleteObject();
    };

    //Execute the delete operation
    clientContext.executeQueryAsync(deleteSucceeded, deleteFailed);
}, fail_handler);  

Note:

In your case, the set_viewXml should be something like below, if you get syntax error use camel query builder tool.

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'CorrespondingListID\'/>' + 
    '<Value Type=\'Text\'>somevaluefrom CorrespondingListID</Value></Eq></Where></Query></View>');
added 885 characters in body
Source Link
SP 2022
  • 4.1k
  • 3
  • 18
  • 41
  var clientContext = new SP.ClientContext( siteURL );  
spList = clientContext.get_web().get_lists().getByTitle( myListName );  

//Create a CAML-query with your filter conditions  
var camlQuery = new SP.CamlQuery();  
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + 
    '<Value Type=\'Number\'>30</Value></Eq>
    <Eq><FieldRef Name=\'Country\'/>' + 
    '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>');  

//The query will return a collection of items matching your conditions  
this.collListItem = spList.getItems(camlQuery);  

clientContext.load(collListItem);  

//Execute the query  
clientContext.executeQueryAsync(function () {

    var itemCount = collListItem.get_count();
    //For each list item in the collection, mark it to be deleted
    for (var i = itemCount - 1; i >= 0; i--) {
        var oListItem = collListItem.itemAt(i);
        oListItem.deleteObject();
    };

    //Execute the delete operation
    clientContext.executeQueryAsync(deleteSucceeded, deleteFailed);  
}, fail_handler);  
  function getItemsToDelete () {
    //You might have to modify this so it filters correctly on your columns
    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(" + myListName + ")/items?$filter=Age eq 30 and Country eq 'US'")

    //Return and ajax request (promise)
    return $.ajax({
        url: requestUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(result) {
            $.each(result.d.results, function(index, item){
                //Note that we push the ajax-request to the array
                //that has been declared a bit down
                itemsToDelete.push(deleteItem(item));
            });            
        },
        error: function(error) {
            //Something went wrong when retrieving the list items
        }
    }); }   
}

function deleteItem (item) {
    //All SP.ListItems holds metadata that can be accessed in the '__metadata' attribute
    var requestUrl = item.__metadata.uri;

    return $.ajax({
        url: requestUrl,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": item.__metadata.etag,
            "X-HTTP-Method": "DELETE"
        },
        success: function() {
            console.log("Item with ID " + item.__metadata.id + " successfully deleted!");
        },
        error: function(error) {
            //Something went wrong when trying to delete the item
        }
    }); }   
}


//Declare an array of deferred objects that hold a delete request  
//for each item that is to be deleted  
var itemsToDelete = [];  

//First get the items to delete  
$.when(getItemsToDelete()).then(function () {
    $.when.apply($, itemsToDelete).then(function(){
        console.log("All items are deleted!");
    });  
});
  

SimilarA similar question is discussed here(source):

  var clientContext = new SP.ClientContext( siteURL ); spList = clientContext.get_web().get_lists().getByTitle( myListName ); //Create a CAML-query with your filter conditions var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + '<Value Type=\'Number\'>30</Value></Eq> <Eq><FieldRef Name=\'Country\'/>' + '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>'); //The query will return a collection of items matching your conditions this.collListItem = spList.getItems(camlQuery); clientContext.load(collListItem); //Execute the query clientContext.executeQueryAsync(function () { var itemCount = collListItem.get_count(); //For each list item in the collection, mark it to be deleted for (var i = itemCount - 1; i >= 0; i--) { var oListItem = collListItem.itemAt(i); oListItem.deleteObject(); }; //Execute the delete operation clientContext.executeQueryAsync(deleteSucceeded, deleteFailed); }, fail_handler);
  function getItemsToDelete () { //You might have to modify this so it filters correctly on your columns var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(" + myListName + ")/items?$filter=Age eq 30 and Country eq 'US'") //Return and ajax request (promise) return $.ajax({ url: requestUrl, type: "GET", headers: { "accept": "application/json;odata=verbose", }, success: function(result) { $.each(result.d.results, function(index, item){ //Note that we push the ajax-request to the array //that has been declared a bit down itemsToDelete.push(deleteItem(item)); }); }, error: function(error) { //Something went wrong when retrieving the list items } }); } function deleteItem (item) { //All SP.ListItems holds metadata that can be accessed in the '__metadata' attribute var requestUrl = item.__metadata.uri; return $.ajax({ url: requestUrl, type: "POST", headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": item.__metadata.etag, "X-HTTP-Method": "DELETE" }, success: function() { console.log("Item with ID " + item.__metadata.id + " successfully deleted!"); }, error: function(error) { //Something went wrong when trying to delete the item } }); } //Declare an array of deferred objects that hold a delete request //for each item that is to be deleted var itemsToDelete = []; //First get the items to delete $.when(getItemsToDelete()).then(function () { $.when.apply($, itemsToDelete).then(function(){ console.log("All items are deleted!"); }); });

Similar question is discussed here(source):

var clientContext = new SP.ClientContext( siteURL ); 
spList = clientContext.get_web().get_lists().getByTitle( myListName ); 

//Create a CAML-query with your filter conditions 
var camlQuery = new SP.CamlQuery(); 
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + 
    '<Value Type=\'Number\'>30</Value></Eq>
    <Eq><FieldRef Name=\'Country\'/>' + 
    '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>'); 

//The query will return a collection of items matching your conditions 
this.collListItem = spList.getItems(camlQuery); 

clientContext.load(collListItem); 

//Execute the query 
clientContext.executeQueryAsync(function () {

    var itemCount = collListItem.get_count();
    //For each list item in the collection, mark it to be deleted
    for (var i = itemCount - 1; i >= 0; i--) {
        var oListItem = collListItem.itemAt(i);
        oListItem.deleteObject();
    };

    //Execute the delete operation
    clientContext.executeQueryAsync(deleteSucceeded, deleteFailed); 
}, fail_handler);  
function getItemsToDelete () {
    //You might have to modify this so it filters correctly on your columns
    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(" + myListName + ")/items?$filter=Age eq 30 and Country eq 'US'")

    //Return and ajax request (promise)
    return $.ajax({
        url: requestUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(result) {
            $.each(result.d.results, function(index, item){
                //Note that we push the ajax-request to the array
                //that has been declared a bit down
                itemsToDelete.push(deleteItem(item));
            });            
        },
        error: function(error) {
            //Something went wrong when retrieving the list items
        }
    });    
}

function deleteItem (item) {
    //All SP.ListItems holds metadata that can be accessed in the '__metadata' attribute
    var requestUrl = item.__metadata.uri;

    return $.ajax({
        url: requestUrl,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": item.__metadata.etag,
            "X-HTTP-Method": "DELETE"
        },
        success: function() {
            console.log("Item with ID " + item.__metadata.id + " successfully deleted!");
        },
        error: function(error) {
            //Something went wrong when trying to delete the item
        }
    });    
}


//Declare an array of deferred objects that hold a delete request 
//for each item that is to be deleted 
var itemsToDelete = []; 

//First get the items to delete 
$.when(getItemsToDelete()).then(function () {
    $.when.apply($, itemsToDelete).then(function(){
        console.log("All items are deleted!");
    }); 
});
  

A similar question is discussed here(source):

Source Link
SP 2022
  • 4.1k
  • 3
  • 18
  • 41

You can use the camel query to get the list item based on your column value which will load only those list items which meets that criteria,then do the delete operation as usual way.

Sample Example : Using JSOM

To filter SP.ListItem's on mulitple conditions, there are no single methods that take the arguments as multiple filter fields. Instead, you'll have to resort to using a CAML query to specify the list items you want, as below.

  var clientContext = new SP.ClientContext( siteURL ); spList = clientContext.get_web().get_lists().getByTitle( myListName ); //Create a CAML-query with your filter conditions var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Age\'/>' + '<Value Type=\'Number\'>30</Value></Eq> <Eq><FieldRef Name=\'Country\'/>' + '<Value Type=\'Text\'>US</Value></Eq></And></Where></Query><RowLimit>10</RowLimit></View>'); //The query will return a collection of items matching your conditions this.collListItem = spList.getItems(camlQuery); clientContext.load(collListItem); //Execute the query clientContext.executeQueryAsync(function () { var itemCount = collListItem.get_count(); //For each list item in the collection, mark it to be deleted for (var i = itemCount - 1; i >= 0; i--) { var oListItem = collListItem.itemAt(i); oListItem.deleteObject(); }; //Execute the delete operation clientContext.executeQueryAsync(deleteSucceeded, deleteFailed); }, fail_handler);

Sample Example : using REST API

  function getItemsToDelete () { //You might have to modify this so it filters correctly on your columns var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle(" + myListName + ")/items?$filter=Age eq 30 and Country eq 'US'") //Return and ajax request (promise) return $.ajax({ url: requestUrl, type: "GET", headers: { "accept": "application/json;odata=verbose", }, success: function(result) { $.each(result.d.results, function(index, item){ //Note that we push the ajax-request to the array //that has been declared a bit down itemsToDelete.push(deleteItem(item)); }); }, error: function(error) { //Something went wrong when retrieving the list items } }); } function deleteItem (item) { //All SP.ListItems holds metadata that can be accessed in the '__metadata' attribute var requestUrl = item.__metadata.uri; return $.ajax({ url: requestUrl, type: "POST", headers: { "accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "IF-MATCH": item.__metadata.etag, "X-HTTP-Method": "DELETE" }, success: function() { console.log("Item with ID " + item.__metadata.id + " successfully deleted!"); }, error: function(error) { //Something went wrong when trying to delete the item } }); } //Declare an array of deferred objects that hold a delete request //for each item that is to be deleted var itemsToDelete = []; //First get the items to delete $.when(getItemsToDelete()).then(function () { $.when.apply($, itemsToDelete).then(function(){ console.log("All items are deleted!"); }); });

Similar question is discussed here(source):

SharePoint 2013 ClientContext : How to DELETE specific list items by MULTIPLE CONDITION filter?