Saturday, April 4, 2009

How to delete data from Sharepoint List using API?

Normally once you delete the data from the list it goes to Recycle Bin. During reading SDK I came to know about a method  'Delete' which deletes data permanently, does not send it recycle bin. So now we have two different approach for deleting list data.

Approach 1 - Delete Data Permanently

using (SPSite siteCollection = new SPSite("http://portal/"))
{
    using (SPWeb site = siteCollection.OpenWeb())
    {
       SPList list1 = site.Lists["MyList"];
       for (int i = list1.Items.Count - 1; i >= 0; i--)
      {
          list1.Items.Delete(i);
      }
    }
}

Approach 2 - Delete Data and it goes to recycle bin

using (SPSite siteCollection = new SPSite("http://portal/"))
{
   using (SPWeb site = siteCollection.OpenWeb())
   {
   SPList list1 = site.Lists["MyList"];
   for (int i = list11.Items.Count - 1; i >= 0; i--)
   {
      SPListItem olistitem = list11.Items[i];
      olistitem.Recycle();
   }
   }
}

0 comments:

Post a Comment