SharePoint List Web-Services, delete all items

June 10, 2011

I’ve been looking for an efficient way to delete all items in a list using the SharePoint list webservice, a few friends have helped me with some good references to get it done by using CAML (@AhmedIG) or by using CSOM (Haytham), you can check them out and see if they solve the problem under your constrains, the challenging part of the problem is you usually don’t produce the item IDs for a list and just allow the SP to do the job for you, you also might discard maintaining the item IDs in your newly created or loaded items in your items collection.

In order to create the below XML batch we will need to build the delete method tags using the existing items’ IDs, so additional IDs retrieval would be required to create a single delete batch (using the GetListItems web service method, as long as we are using the SP list web-service only in our context)

   1: <Batch>

   2:   <Method ID='1' Cmd='Delete'>

   3:     <Field Name='ID'> TheMissingID </Field>

   4:   </Method>

   5: </Batch>

The solution starts here, a method returns an ArrayList of item IDs, to be used by the batch delete method. The GetListIDs method uses the GetListItems web service method to retrieve one field only – the Item ID – of all the items in the list.

   1: public static ArrayList GetListIDs(String ListName)

   2: {

   3:     WS_TopDealLists.Lists listService = new WS_TopDealLists.Lists();

   4:     listService.Credentials =

   5:         new NetworkCredential("username", "PASS****", "domain");

   6:     listService.Url = 

   7:     "http://SPServer/sites/SiteCollection/sandbox/_vti_bin/Lists.asmx";

   8:  

   9:     XmlDocument xmlDoc = new System.Xml.XmlDocument();

  10:  

  11:     XmlNode ndViewFields =

  12:         xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

  13:     ndViewFields.InnerXml = "<FieldRef Name='ID' />";

  14:  

  15:     XmlNode ndListItems =

  16:         listService.GetListItems(ListName, null, null,

  17:         ndViewFields, null, null, null);

  18:  

  19:     //convert String to XMLReader

  20:     XmlReaderSettings settings = new XmlReaderSettings();

  21:     settings.ConformanceLevel = ConformanceLevel.Fragment;

  22:     settings.IgnoreWhitespace = true;

  23:     settings.IgnoreComments = true;

  24:     XmlReader xmlReader = 

  25:     XmlReader.Create(new StringReader(ndListItems.OuterXml), settings);

  26:  

  27:     ArrayList IDsList = new ArrayList();

  28:  

  29:     while (xmlReader.Read())

  30:     {

  31:         if (xmlReader.Name == "z:row")

  32:             IDsList.Add(xmlReader.GetAttribute("ows_ID").ToString());

  33:     }

  34:  

  35:     return IDsList;

  36: }

And here’s the Delete method which already takes the IDs ArrayList returned by the GetListIDs method then constructs one delete XML batch, and we’re done, all items are deleted in one shot.

   1: public static void DeleteItems(String ListName, ArrayList IDs)

   2: {

   3:     if (IDs.Count == 0)

   4:         return;

   5:  

   6:     WS_TopDealLists.Lists listService = new WS_TopDealLists.Lists();

   7:     listService.Credentials = 

   8:     new NetworkCredential("username", "PASS****", "domain");

   9:     listService.Url = 

  10:     "http://SPServer/sites/SiteCollection/sandbox/_vti_bin/Lists.asmx";

  11:  

  12:     string strBatch = "";

  13:  

  14:     foreach (String ID in IDs)

  15:     {

  16:         strBatch += "<Method ID='1' Cmd='Delete'>" +

  17:             "<Field Name='ID'>" + ID + "</Field></Method>";

  18:     }

  19:  

  20:     XmlDocument xmlDoc = new System.Xml.XmlDocument();

  21:     XmlElement elBatch = xmlDoc.CreateElement("Batch");

  22:  

  23:     elBatch.InnerXml = strBatch;

  24:  

  25:     listService.UpdateListItems(ListName, elBatch);

  26: }

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 130 other followers