Online Training On SharePoint
                      

Monday, 4 May 2009

Different approaches of deleting Data from SharePoint List using SharePoint Object Model

Recently while I was was working with SharePoint Object Model to delete data from SharePoint List I observed that the data was deleting permanently and was not going to Recycle Bin. We were using the following code:

using (SPSite siteCollection = new SPSite("http://localhost"))
{
using (SPWeb site = siteCollection.OpenWeb())
{
SPList list1 = site.Lists["TestList"];

for (int i = list1.Items.Count - 1; i >= 0; i--)
{
list1.Items.Delete(i);
}
}
}

The method deletes the data permanently from the SharePoint and it does not send the data to Recycle Bin.

So upon doing little search I found Recycle method of SPListItem Class. This method will delete the data from the list and send the data to the Recycle Bin.

So to delete the data with SharePoint Object Model from a SharePoint List so that the deleted data goes to Recycle Bin use the following code:

using (SPSite siteCollection = new SPSite("http://localhost"))
{
using (SPWeb site = siteCollection.OpenWeb())
{
SPList list1 = site.Lists["TestList"];

for (int i = list11.Items.Count - 1; i >= 0; i--)
{
SPListItem olistitem = list11.Items[i];

olistitem.Recycle();
}
}
}

No comments:

Related Posts with Thumbnails