In my previous post SharePoint List C# Part 1 I discussed how we can add items to SharePoint list. We also have to delete items from a list.
Following C# code can be used in your custom webpart or feature to delete item from SharePoint list. Here I am going to find the items to delete using CAML query.
public void DeleteListItem() { // choose your site string strUrl = "http://mysite:5050/"; using (SPSite site = new SPSite(strUrl)) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["List_A"]; SPListItemCollection itemCollection; SPQuery oQuery = new SPQuery(); oQuery.Query = "<where><eq><fieldref Name='Title' /><value Type='Text'>ABC</Value></Eq></Where>"; itemCollection = list.GetItems(oQuery); List<splistitem> valuesToDelete = new List<splistitem>(); foreach (SPListItem item in itemCollection) { valuesToDelete.Add(item); } web.AllowUnsafeUpdates = true; foreach (SPListItem itm in valuesToDelete) { itm.Delete(); } list.Update(); web.AllowUnsafeUpdates = false; } } }
In the above example I have use a CAML query to delete items in the Task list which title equal to ABC. As above you have to maintain separate list to stote items that you are goint to delete, otherwise it will not work.
Thanks very much been struggling trying to use ProcessBatchData to achieve this and getting nowhere - I'll go get my CAML book out and finish my project now :)
ReplyDelete@Stu,
ReplyDeleteThanks for your comment, you can try U2U CAML Query Builder there.
Although the general idea of what you're doing is correct, your code is a bit odd.
ReplyDeleteThe only place where you're doing a try/catch is around the part where you're adding items to a List, which is something that shouldn't throw an exception.
You should add a try/catch around the part where you're doing the query and for each item you're actually deleting.
The additional check for "if(item != null)" is unnecessary, since you're just looping through a collection, which can't contain nulls. Adding the try/catch here, is a performance penalty and it's in the wrong place, like I mentioned before.
Oh, and you should add using() blocks around where you create new SPSite and SPWeb objects.
I think you could tidy it up a bit by doing this instead:
web.AllowUnsafeUpdates = true;
try
{
SPListItemCollection itemCollection = list.GetItems(oQuery);
for(var i = itemCollection.Count - 1; i >= 0; i--)
{
try
{
itemCollection[i].Delete();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
web.AllowUnsafeUpdates = false;
@Dennis,
ReplyDeleteThanks for sharing your idea, I'll update my code.
I have worked on SharePoint Lists, but don't started to work on such customization from code behind, it will help me in near future to do such coding.
ReplyDeleteI believe that you can use the FOR loop instead of a foreach for deleting items from a list. This method would stop the enumeration from changing and thus allow you to Delete() right then and there in a single loop. :)
ReplyDeleteHi guys, I would use a while loop for more compact code
ReplyDeletewhile (0 < itemCollection.Count) {
itemCollection[0].Delete();
}
Its deleting all the items in the list any help...?
ReplyDelete