Sunday, October 31, 2010

Delete data items from Sharepoint list

Following code help you to delete the Sharepoint list items row by row.

private void DeleteRows()
{

string SPSiteName = "http://localhost";
string SPWebName = "";
string SPListName = "some_list";

try
{
using (SPSite site = new SPSite(SPSiteName))
{
using (SPWeb web = site.AllWebs[SPWebName])
{
SPList list = web.Lists[SPListName];
SPListItemCollection listitems = web.Lists[SPListName].Items;


int count = 0;

for (int i = list.Items.Count - 1; i >= 0; i--)
{
listitems.Delete(i);
count++;
}
MessageBox.Show(count+" rows deleted successfully");

}
}
}
catch (Exception ee)
{
MessageBox.Show("Incorrect Data");
}
}

No comments:

Post a Comment