2

Plain and simple, I'm trying to delete a massive amount of items from a list as quickly as possible using a c# app, and have seen references to batch processes being the best way to do this. Not knowing much about them I've used examples online from places like this but keep running into the error <nativehr>0x80070057</nativehr><nativestack></nativestack> when the application reaches the SPWeb.ProcessBatchData step. Here is my code:

SPListItemCollection oCol = oList.Items;
StringBuilder sbDelete = new StringBuilder("<Batch>");
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"? >");
sbDelete.Append("<ows:Batch OnError=\"Return\">");
for (int x = oCol.Count - 1; x >= 0; x--)
{
    sbDelete.Append("<Method>");
    sbDelete.Append("<SetList Scope=\"Request\">" + oList.ID.ToString() + "</SetList>");
    sbDelete.Append("<SetVar Name=\"ID\">" + oCol[x].ID.ToString() + "</SetVar>");
    sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
    sbDelete.Append("</Method>");
}
sbDelete.Append("</ows:Batch>");
oWeb.AllowUnsafeUpdates = true;
oWeb.ProcessBatchData(sbDelete.ToString());

Can anyone spot what I'm doing wrong? I've tried multiple approaches recommended by sources like this and this but with no luck. The only theory I have is that the batch ends up being too big, but if so, what is the limit?

1

When I use web service operations for batch processing in 2010, my batch looks much different:

    <Batch OnError='Continue'>
      <Method ID='1' Cmd='Delete'>
        <Field Name='ID'>1234</Field>
      </Method>
<Method ID='2' Cmd='Delete'>
        <Field Name='ID'>1235</Field>
      </Method>
    </Batch>

I don't know if that makes a difference in 2013 and in your implementation.

2
  • I'll restructure the batch to look more like yours and see if that helps. I can't find any documentation on the differences between 10 and 13 in this regard, so it's pretty much just a "guess and check" situation unfortunately – thanby Mar 3 '14 at 17:23
0

This one is for Google search users like me:

    public static void DeleteItemsUsingBatch(SPWeb web, String ListName)
    {

        bool allowUnsafe = web.AllowUnsafeUpdates;

        web.AllowUnsafeUpdates = true;
        SPList list = web.Lists[ListName];

        SPListItemCollection listItem = list.GetItems(new SPQuery());
        StringBuilder sbDelete = new StringBuilder();
        sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

        int methodID = 0;
        foreach (SPListItem item in listItem)
        {
            sbDelete.AppendFormat("<Method ID=\"{0}\">" +
                                          "<SetList>{1}</SetList>" +
                                          "<SetVar Name=\"ID\">{2}</SetVar>" +
                                          "<SetVar Name=\"Cmd\">Delete</SetVar>" +
                                          "</Method>",
                                          methodID, //{0}
                                          list.ID, //{1}
                                          item.ID //{2}
                                          );
            methodID++;
        }
        sbDelete.Append("</Batch>");

        string delete = sbDelete.ToString();

        web.ProcessBatchData(delete);
        web.AllowUnsafeUpdates = allowUnsafe;
    }
2
  • What is your source for that? It looks pretty much the same as one of the sources I linked (though apparently the link is now broken). – thanby May 14 '14 at 11:44
  • I dont remember where i got it, i had it in my personal util class for a long time. "<Method ID=\"{0}\">" <-- method in your code was missing an ID attribute. i suspect that was the reason for error. – dotsa May 14 '14 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.