SharePoint 批量删除SPListitem

用item.Delete 来删除 ,如果行数在10多行的时候,删除所需要的时间会达到好几1-2秒,所以非常之慢!一般来说,当行数多的时候会采取以下的方法来删除

public void DeleteData(SPListItemCollection items, SPList itemlist)

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite site = new SPSite(itemlist.ParentWeb.Site.Url))

                {

                    SPWeb web = site.OpenWeb(itemlist.ParentWeb.ID);

                    SPList list = web.Lists[itemlist.ID];



                    StringBuilder sbDelete = new StringBuilder();

                    sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

                    foreach (SPListItem item in items)

                    {

                        sbDelete.Append("<Method>");

                        sbDelete.Append("<SetList Scope=\"Request\">" + list.ID + "</SetList>");

                        sbDelete.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");

                        sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");

                        sbDelete.Append("</Method>");

                    }

                    sbDelete.Append("</Batch>");

                    web.ProcessBatchData(sbDelete.ToString());

                }

            });

        }

  

你可能感兴趣的:(SharePoint)