5

I am trying to use batch update for one of the list in SP2013 site. I am trying to run a simple batch request as follows.

        using (var spSite = new SPSite(siteName))
        using (var spWeb = spSite.OpenWeb())
        {

            var clientList = spWeb.Lists["Clients"];


            string batchFormat = string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<ows:Batch OnError=\"Return\">"
            +"<Method ID=\"{0}\">" +
             "<SetList>{1}</SetList>" +
             "<SetVar Name=\"Cmd\">Save</SetVar>" +
             "<SetVar Name=\"ID\">{2}</SetVar>" +
             "<SetVar Name=\"InstitutionType\">{3}</SetVar>" +
            "</Method>"+"</ows:Batch>", 1, clientList.ID,833, "None");

            spWeb.AllowUnsafeUpdates = true;
            spWeb.ProcessBatchData(batchFormat);

        }

And I am getting response of spWeb.ProcessBatchData(batchFormat); as

<Results><Result ID="1" Code="0">
</Result>
</Results>

Can anyone please suggest me what is the correct way to perform batch updates. It runs but nothing is updated in list.

Thanks.

4

In your code you have set ID 833, so it will only update the item which has ID 833.

Modify your code as below:

using (SPSite spSite = new SPSite(siteName))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
        spWeb.AllowUnsafeUpdates = true;

        StringBuilder methodBuilder = new StringBuilder();
        string batch = string.Empty;
        string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
        "<Batch OnError=\"Return\">{0}</Batch>";

        string methodFormat = "<Method ID=\"{0}\">" +
        "<SetList>{1}</SetList>" +
        "<SetVar Name=\"Cmd\">Save</SetVar>" +
        "<SetVar Name=\"ID\">{2}</SetVar>" +
        "<SetVar Name=\"urn:schemas-microsoft-com:office:office#InstitutionType\">{3}</SetVar>" +
        "</Method>";

        SPList list = spWeb.Lists.TryGetList("Clients");
        if(list!=null)
        {
            SPQuery query = new SPQuery();
            query.Query = "<View/>";
            SPListItemCollection listItem = list.GetItems(query);

            foreach (SPListItem item in listItem)
            {
                methodBuilder.AppendFormat(methodFormat, item.ID, list.ID, item.ID, "None");
            }

            batch = string.Format(batchFormat, methodBuilder.ToString());

            // Process the batch of commands.
            string batchReturn = spWeb.ProcessBatchData(batch);
            spWeb.AllowUnsafeUpdates = false;
        }
    }
}
  • I am trying to update for single item only, although I will modify it for large number of items but for now I am concerned about the fact that it is not working for single item and will it work for large number of items. – Nikhil Chavan Feb 16 '17 at 12:16
  • hmm if you are updating a single item why use batch ? :) normally i use list.GetItemById(1) and then update the metadata. Batch should be used for bulk updates ! – Gautam Sheth Feb 16 '17 at 12:18
  • Am I missing something in syntax or any other thing, This single item should have been updated, but nothing is updated for it. – Nikhil Chavan Feb 16 '17 at 12:18
  • I used it for multiple items, but then it didn't worked for multiple items. Then I tried to do some POC kind of stuff and modified it to update only single item, even ID is hardcoded here. But still it didn't worked. – Nikhil Chavan Feb 16 '17 at 12:20
  • not 100% sure about this, but your code uses <ows:Batch whereas I am using <Batch that could be the difference. But again not sure about that, otherwise your code looks good. I have referenced this link - msdn.microsoft.com/en-us/library/office/ms437562.aspx – Gautam Sheth Feb 16 '17 at 12:34

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.