Tuesday, September 1, 2015

Deleting List Items Using ProcessBatchData

private static void DeleteItems(){
using (SPSite oSite = new SPSite(<SiteUrl>))
                    {
                        using (SPWeb oWeb = oSite.OpenWeb())
                        {
                            SPList oList = oWeb.Lists[<ListName>];
                            SPQuery oQuery = new SPQuery();
                            oQuery.Query = "<Where><Eq><FieldRef Name='<ColumnName>' /><Value Type='Text'>"<ColumnValue>"</Value></Eq></Where>";
                          
                            SPListItemCollection listItem = oList.GetItems(oQuery);                          
                            StringBuilder sbDelete = new StringBuilder();
                            string xmlFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                            sbDelete.Append(xmlFormat);
                            sbDelete.Append("<Batch>");
                            string buildQuery = "<Method><SetList Scope=\"Request\">" + oList.ID + "</SetList>";
                            buildQuery = buildQuery +
                            "<SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>"; foreach (SPListItem item in listItem)
                            {
                                sbDelete.Append(string.Format(buildQuery, item.ID.ToString()));
                            }
                            sbDelete.Append("</Batch>");
                            oWeb.ProcessBatchData(sbDelete.ToString());
                        }
                    }
                });
}

No comments:

Post a Comment