Deleting a large number of items from SharePoint list, the best way I found was to use "SPContext.Current.Site.RootWeb.ProcessBatchData" as it avoided the API and was considerably faster.
I have created a Console utility to achieve this. This utility accepts two arguments:
1. Server URL
2. List Name
Below is the full code of the utility:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace DeleteAllItemsFromList
{
class Program
{
static void Main(string[] args)
{
string serverUrl = string.Empty;
string listName = string.Empty;
if (args.Count() > 1)
{
try
{
//Read input from Console
//First argument as ServerURL
serverUrl = args[0];
//Second argument as ListName
listName = args[1];
using (SPSite site = new SPSite(serverUrl))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists[listName];
SPListItemCollection splic = list.Items;
StringBuilder batchString = new StringBuilder();
batchString.Append("");
foreach (SPListItem item in splic)
{
batchString.Append("");
batchString.Append("" + Convert.ToString(item.ParentList.ID) + " ");
batchString.Append("" + Convert.ToString(item.ID) + " ");
batchString.Append("Delete ");
batchString.Append("
}
batchString.Append("
");
web.ProcessBatchData(batchString.ToString());
}
}
Console.WriteLine("Done.");
}
catch (Exception exc)
{
Console.WriteLine("ERROR:" + exc.Message);
}
}
else
{
Console.WriteLine("ERROR: Please provide serverUrl[0] and listName[1] as arguments.");
}
}
}
}
Happy Coding!!!
No comments:
Post a Comment