Tuesday, October 6, 2009

Delete item from SharePoint List


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.

8 comments:

  1. 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
  2. Although the general idea of what you're doing is correct, your code is a bit odd.

    The 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;

    ReplyDelete
  3. @Dennis,
    Thanks for sharing your idea, I'll update my code.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. I 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. :)

    ReplyDelete
  6. Hi guys, I would use a while loop for more compact code

    while (0 < itemCollection.Count) {
    itemCollection[0].Delete();
    }

    ReplyDelete
  7. Its deleting all the items in the list any help...?

    ReplyDelete