Wednesday, August 1, 2012

Batch Update and Delete in SharePoint List based on conditions.

Here I am going to give you an example that how can you use batch update and batch delete is SharePoint List programmatically. It deletes all selected items as per given query and list name. It is so time efficient in comparison to delete or update them one by them.

For Updating Items :

public void BatchUpdateOfColumn(SPWeb objSPWeb, string listName, string Query, string ValueToUpdate)
        {
            if (objSPWeb != null)
            {
                // Set up the variables to be used.
                StringBuilder methodBuilder = new StringBuilder();
                string batch = string.Empty;

                string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                  "<ows:Batch OnError=\"Return\">{0}</ows: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#Exclude\">{3}</SetVar>" +
                 "</Method>";

                // Get the list containing the items to update.
                if (listName != null)
                {
                    SPList list = objSPWeb.Lists[listName];
                    if (list != null)
                    {
                        string listGuid = list.ID.ToString();
                        // Query to get the unprocessed items.
                        SPQuery query = new SPQuery();
                        query.Query = Query;
                        query.ViewAttributes = "Scope='Recursive'";
                        SPListItemCollection unprocessedItems = list.GetItems(query);
                        // Build the CAML update commands.
                        for (int i = 0; i < unprocessedItems.Count; i++)
                        {
                            int itemID = unprocessedItems[i].ID;
                            methodBuilder.AppendFormat(methodFormat, itemID, listGuid, itemID, ValueToUpdate);
                         }

                        // Put the pieces together.
                        batch = string.Format(batchFormat, methodBuilder.ToString());

                        // Process the batch of commands.
                        string batchReturn = objSPWeb.ProcessBatchData(batch);
                    }
                }
            }
          }

For Deleting  Items:

 public static void DeleteItemUsingBatch(string strUrl)
        {
            using (SPSite site = new SPSite(strUrl))
            {
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                SPList list = web.Lists["List Name"];
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><IsNull><FieldRef Name='Coulmn1' /></IsNull><IsNull><FieldRef Name='Coulmn2' /></IsNull></And></Where>"; // Query as per need.
                query.ViewAttributes = "Scope='Recursive'"; // Because items are within different folders.
                SPListItemCollection listItem = list.GetItems(query);
                StringBuilder sbDelete = new System.Text.StringBuilder();
                sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
                string command = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList>" +
                    "<SetVar Name=\"ID\">{0}</SetVar>" +
                    "<SetVar Name=\"Cmd\">Delete</SetVar></Method>";
                foreach (SPListItem item in listItem)
                {
                    sbDelete.Append(String.Format(command, Convert.ToString(item.ID)));
                }
                sbDelete.Append("</Batch>");
                web.ProcessBatchData(sbDelete.ToString());
            }
        }

No comments:

Post a Comment