SharePoint Batch Job Troubleshooting
The most convenience way to work on huge items in SharePoint is, using the batch job. Developer gets stuck in foreach loop to do the operation on multiple items in the list and libraries.
Well whenever working on large number of items whether its list or library using the foreach loop or any other loop in code will occupy high memory and of course this could affect your complete site performance.
Batch job is xml query which is used to process huge number of list and library items. Have you tried deleting 100 items from the lists? Well try it and then use the batch job to do the same. You will see the results. Normal coding will take 2 to 5 sec to delete these no of items whereas batch job will done within second. Surprised? yaw that’s the reason to go for the batch job. J
Below is the simple code to use the batch job, and below that I described the issues you might get.
Deleting Items from the Lists-
Use this standard function in your code, pass the spquery and list name and it will delete the specified items from the list.
StringBuilder strBuilerBatchCMD = BatchJobDelete(SPQuery spquery, SPList splist)
spweb.AllowUnsafeUpdates = true;
string strBatchJobOutPut = spsite.RootWeb.ProcessBatchData(strBuilerBatchCMD.ToString());
spweb.AllowUnsafeUpdates = false;
spweb.Close();
spsite.Dispose();
public StringBuilder BatchJobDelete(SPQuery spquery, SPList splist)
{
try
{
SPListItemCollection items = splist.GetItems(spquery);
StringBuilder sb = new StringBuilder();
sb.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string batchCommand = “<Method><SetList Scope=\”Request\”>” + splist.ID + “</SetList><SetVar Name=\”ID\”>{0}</SetVar><SetVar
Name=\”Cmd\”>Delete</SetVar></Method>”;
foreach (SPListItem item in items)
{
sb.Append(string.Format(batchCommand, item.ID.ToString()));
}
sb.Append(“</Batch>”);
}
catch (Exception ex)
{
//WRITE ERROR TO LOG FILE
logError(ex);
return null;
}
finally
{
//RELEASE OBJECTS HERE
}
}
Well above code will help you to delete the items from the SharePoint List.
Deleting Items from the Document Library –
Below is the batch script for to delete the times from the SharePoint Document library.
sb.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string batchCommand = “<Method><SetList Scope=\”Request\”>” + splist.ID + “</SetList><SetVar Name=\”ID\”>{0}</SetVar><SetVar
Name=\”Cmd\”>Delete</SetVar><SetVar Name=\”owsfileref\”>{1}</SetVar></Method>”;
foreach (SPListItem item in items)
{
sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);
}
sb.Append(“</Batch>”);
TroubleShooting-
When you execute the above code it will work perfectly, however there might be scenario where you will come across issue that the process batch job is successfully applied however items are not affected.
The reason behind is batch command. I tried below syntax which failed
Failed Case 1 (Delete is in capital case)
sb.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string batchCommand = “<Method><SetList Scope=\”Request\”>” + splist.ID + “</SetList><SetVar Name=\”ID\”>{0}</SetVar><SetVar
Name=\”Cmd\”>DELETE</SetVar><SetVar Name=\”owsfileref\”>{1}</SetVar></Method>”;
foreach (SPListItem item in items)
{
sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);
}
sb.Append(“</Batch>”);
Failed Case 2 (command is in small caps)
sb.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string batchCommand = “<method><setlist scope=\”Request\”>” + splist.ID + “</setList><setvar name=\”ID\”>{0}</setvar><setvar
name=\”Cmd\”>Delete</setvar><setvar name=\”owsfileref\”>{1}</setvar></method>”;
foreach (SPListItem item in items)
{
sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);
}
sb.Append(“</Batch>”);
Failed Case 3 (Batch is capital)
sb.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><BATCH>”);
string batchCommand = “<method><setlist scope=\”Request\”>” + splist.ID + “</setList><setvar name=\”ID\”>{0}</setvar><setvar
name=\”Cmd\”>Delete</setvar><setvar name=\”owsfileref\”>{1}</setvar></method>”;
foreach (SPListItem item in items)
{
sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);
}
sb.Append(“</BATCH>”);
Inorder to get it work you should try using the command which is specified above under section – Deleting Items from the Document Library
Also what I realised is if use same case for Batch and Delete then small case command will also work.
For successfully execution the processbatchjob method returns the xml as
<Result ID=”” Code=”0″></Result>
<Result ID=”” Code=”0″></Result>
For failed process batch execution it returns the xml format as
<method><setlist scope=”Request”>53361993-4390-4947-887a-199251d24c69</setlist><setvar name=”ID”>4899</setvar><setvar name=”Cmd”>Delete</setvar><setvar name=”owsfileref”>/Directory/005.xlsx</setvar></method>
<method><setlist scope=”Request”>53361993-4390-4947-887a-199251d24c69</setlist><setvar name=”ID”>4900</setvar><setvar name=”Cmd”>Delete</setvar><setvar name=”owsfileref”>/Directory/007.xlsx</setvar></method>
If you realized any mistake or mismatch here please comment it.