Deleting Items in a SPListItemCollection
-
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.
-
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.
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