Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. Deleting Items in a SPListItemCollection

Deleting Items in a SPListItemCollection

Scheduled Pinned Locked Moved SharePoint
sharepointxmlquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aptiva Dave
    wrote on last edited by
    #1

    Been having some trouble with doing this and have looked all over the internet for the actual solution. At first I was using a for each loop and deleting individual spitem objects, but that doesn't work since the enumeration changes after each deletion. So, I found a blog post about deleting using the ProcessBatchData method. Here is my code:

    Dim sbDelete As StringBuilder = New StringBuilder
    Dim xmlFormat As String = "<?xml version=""1.0"" encoding=""UTF-8""?>"
    sbDelete.Append(xmlFormat)
    sbDelete.Append("<Batch>")
    Dim buildQuery As String = "<Method><SetList Scope=""Request"">" & fList.ID.ToString & "</SetList>"
    buildQuery = buildQuery & _
    "<SetVar Name=""ID"">{0}</SetVar Name=""Cmd"">Delete</SetVar></Method>"
    For Each x As SPListItem In flItems
    sbDelete.Append(String.Format(buildQuery, x.ID.ToString()))
    Next
    sbDelete.Append("</Batch>")
    web.ProcessBatchData(sbDelete.ToString)

    This runs fine, but when I go to the list where the items where supposed to be deleted from I find that they are still there. This web app is a fuel log for our Flight Operations department. What I'm trying to do is when the Admins delete a plane from the system, SharePoint would then deleted all of the fuel log entries for that plane. I figured it would be simple to do, but either I'm doing something wrong or it isn't as simple as I think.

    J 1 Reply Last reply
    0
    • A Aptiva Dave

      Been having some trouble with doing this and have looked all over the internet for the actual solution. At first I was using a for each loop and deleting individual spitem objects, but that doesn't work since the enumeration changes after each deletion. So, I found a blog post about deleting using the ProcessBatchData method. Here is my code:

      Dim sbDelete As StringBuilder = New StringBuilder
      Dim xmlFormat As String = "<?xml version=""1.0"" encoding=""UTF-8""?>"
      sbDelete.Append(xmlFormat)
      sbDelete.Append("<Batch>")
      Dim buildQuery As String = "<Method><SetList Scope=""Request"">" & fList.ID.ToString & "</SetList>"
      buildQuery = buildQuery & _
      "<SetVar Name=""ID"">{0}</SetVar Name=""Cmd"">Delete</SetVar></Method>"
      For Each x As SPListItem In flItems
      sbDelete.Append(String.Format(buildQuery, x.ID.ToString()))
      Next
      sbDelete.Append("</Batch>")
      web.ProcessBatchData(sbDelete.ToString)

      This runs fine, but when I go to the list where the items where supposed to be deleted from I find that they are still there. This web app is a fuel log for our Flight Operations department. What I'm trying to do is when the Admins delete a plane from the system, SharePoint would then deleted all of the fuel log entries for that plane. I figured it would be simple to do, but either I'm doing something wrong or it isn't as simple as I think.

      J Offline
      J Offline
      JimmyRopes
      wrote on last edited by
      #2

      I find it painful to read VB so I won't try to see where you are going wrong. Here is an alternate way of deleting all items from a list that may not be as efficient but for lists with a reasonable number of items the efficiency will not matter. Go through the list items and get a list of GUIDs of the list items.

      SPListItemCollection listItems = oWeb.Lists["List Name"].Items;
      List listGUID = new List();
      foreach (SPListItem Item in listItems)
      {
      listGUID.Add(Item.UniqueId);
      }

      Translate that to VB. Then go through the list of GUIDs and delete each one. Update the list after you are finished and the items will be gone.

      The report of my death was an exaggeration - Mark Twain
      Simply Elegant Designs JimmyRopes Designs
      Think inside the box! ProActive Secure Systems
      I'm on-line therefore I am. JimmyRopes

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups