delete multiple rows of a list by row ID

Apr 3, 2012 at 8:02 PM

Hello. I'm trying to figure out how I can delete multiple rows of a list if I have an array of row IDs? I see SPUpdateMultipleListItems but I cannot figure out how to use that for what I need.

Apr 4, 2012 at 1:52 PM

You should be looking at the UpdateListItems operation of the Lists service... GIven that you already have an array of ID's it should be rather easy to quickly build your XML with all updates (deletes) and send it through... Also, a quick search on these forums quickly gave me an example... here http://spservices.codeplex.com/discussions/223325

Good luck

Paul

Apr 4, 2012 at 2:21 PM

In that example it deletes one row. I was looking to bulk delete multiple rows in one request.

When you say "build your XML" I assume you mean the CAML which means I would have to send multiple requests, right?

Apr 4, 2012 at 3:10 PM

Not necessary....  Check the example again (the first comment by Marc)... This is how to delete multiples:

<Batch OnError="Continue" ListVersion="1" ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Delete">
      <Field Name='ID'>2</Field>
   </Method>

   <Method ID="2" Cmd="Delete">
      <Field Name='ID'>8</Field>
   </Method>

</Batch>

The example is deleting two records (ID 2 and ID 8). You can find more information regarding the <Batch> XML aggregate in the MS documentation.  Look over the documentation on how to use the UpdateListItems to see how to "plug" the xml above into that operation to get this done.

Paul.

 

Apr 4, 2012 at 3:23 PM

AHHHHHHHHHH

I think I got it.

Will have to try it later when I get my site ready but it looks like I should do:

var u = ''
u += '<Batch OnError="Continue">';
u += '	<Method ID="1" Cmd="Delete"></Method>';
u += '	<Method ID="2" Cmd="Delete"></Method>';
u += '	<Method ID="3" Cmd="Delete"></Method>';
u += '</Batch>';

$().SPServices( {
	operation: "UpdateListItems",
	listName: "TheDingoAteYourBaby",
	updates: u
	completefunc: function( xData, Status ){}
} );

 

Thank you so much sir!

Apr 4, 2012 at 5:29 PM

imthenachoman,

Your XML example isn't valid just yet. You'll need to add a <Field> node to your methods.  The field represents the ID of the list item you want to delete.  The ID of the method is simply a counter for the <Batch> to separate the different operations you'd like to do within your call.  You can actually mix and match your Cmd's within your <Method>'s and do all the operations within one call. e.g. "Delete", "New", "Update"

 

Cheers,

Matt

Apr 4, 2012 at 6:35 PM

Good Catch Matt...

imthenachoman,

I also made that mistake during my first usage of the <Batch> and <Method> elements... the ID attribute on the <Method> element is not the ID of the item in the list... but rather an identifier for the update block... so looking above, you have 3 updates going in 1 single shot... the ID's help with the response case something fails...  It will include that same ID along with a status on each update that was done... so some could have been successful and other failed... you need to check.

Paul.

Apr 4, 2012 at 8:23 PM

Got it. Thanks. So something like:

var u = '' u += '<Batch OnError="Continue">';
u += ' <Method ID="1" Cmd="Delete"><Field name="ID">1</Field></Method>';
u += ' <Method ID="2" Cmd="Delete"><Field name="ID">2</Field></Method>';
u += ' <Method ID="3" Cmd="Delete"><Field name="ID">3</Field></Method>';
u += '</Batch>';

$().SPServices( {
operation: "UpdateListItems",
listName: "TheDingoAteYourBaby",
updates: u
completefunc: function( xData, Status ){}
} );

 

Thanks so much guys!!