Wednesday, June 5, 2013

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 permenently. does not send it recycle bin. So now we have two different approaches to deleting the list data.

Approach1 - Delete Data Permanently:

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

Approach2 - Delete Data and it goes to Recyclebin:

using(SPSite sitecollection = new SPSite("http://portal"))
{
    using(SPweb site = sitecollection.Openweb())
    {  
    SPList listdata = site.Lists["MyList"];
    for(int i=listdata.Items.Count-1;i>=0;i--)
    {
    SPListItem olistitem = listdata.Items[i];
    olistitem.Recycle();
    }
    }
}

No comments:

Post a Comment