Pages

Search This Blog

Sunday, April 22, 2012

SharePoint List Bulk Delete

Hi Friends,

I came through some requirement where I need to delete number of rows on the basis of some condition. Here we have choice of iterating all the list items and match the required condition if matched delete that list record. This is fine when we say its meeting the requirement. But when we look into its performance its not really a good solution. So here we have another very efficient solution of using ProcessBatchData of SPWeb. This batch technique is 10 times faster than list.Delete(i) technique and it minimizes the round trips to DB. Please find a very simple implementation of this you can alter this as per your requirement.


private void button1_Click(object sender, EventArgs e)
        {
            String strSourceList = "Bulk Delete List";//textBox2.Text.Trim();
            String strSite = "http://abcSite:9000/sites/uk/";//textBox1.Text.Trim();
            String strIds = String.Empty;
            try
            {
                if (!String.IsNullOrEmpty(strSite))
                {
                    if (!String.IsNullOrEmpty(strSourceList))
                    {
                        try
                        {
                            SPSite sourceSite = null;
                            try
                            {
                                sourceSite = new SPSite(strSite);
                            }
                            catch { }

                            if (sourceSite != null)
                            {
                                SPWeb sourceWeb = sourceSite.RootWeb;
                                if (sourceWeb != null)
                                {
                                    sourceWeb.AllowUnsafeUpdates = true;

                                    SPList sourceList = sourceWeb.Lists[strSourceList];
                                    StringBuilder sbDelete = new StringBuilder();
                                    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

                                    if (sourceList != null)
                                    {
                                        string command = "<Method>" +
                                                         "<SetList Scope=\"Request\">" + sourceList.ID + "</SetList>" +
                                                         "<SetVar Name=\"ID\">{0}</SetVar>" +
                                                         "<SetVar Name=\"Cmd\">Delete</SetVar>" +
                                                         "</Method>";

                                        foreach (SPListItem objListItem in sourceList.Items)
                                        {
                                            sbDelete.Append(string.Format(command, objListItem.ID.ToString()));
                                        }
                                        sbDelete.Append("</Batch>");

                                        string retVal = sourceWeb.ProcessBatchData(sbDelete.ToString());

                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

Thanks
Prabhat

No comments:

Post a Comment