开发者

How to Delete all the Items from SharePoint List without iteration using SharePoint Webservice

开发者 https://www.devze.com 2023-04-06 11:03 出处:网络
I wanted to delete all the Items from SharePoint List using Lists webservice, i know we can do it by iterating all the items, my problem is that the, i have 25000 items in my list and wanted to delete

I wanted to delete all the Items from SharePoint List using Lists webservice, i know we can do it by iterating all the items, my problem is that the, i have 25000 items in my list and wanted to delete it in one go, Is there any way to do this?

or开发者_运维百科 is there a way to get all the attributes of List then delte the list and re-create the list with the previously saved attributes.

Thanks in Advance.


You could save the list as a template (List Settings -> Save list as template), then delete the list, then create a new list based on the template that you saved.


You could use the UpdateListItems method of the Lists web service, however it is still going to take a long time to delete all items at once. Try the following snippet -

var batch = new StringBuilder();
batch.Append("<Batch OnError='Continue' ListVersion='1' ListName='{GUID}'>");

string itemTemplate = "<Method ID='1' Cmd='Delete'><Field Name='ID'>{0}</Field></Method>";

for (int i = 0; i < 300000; i++)
    batch.Append(string.Format(itemTemplate, i));

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

//query Lists service

This code assumes that your item's ID range is from 0 to 300000 - this will be your only option without iterating all items (and getting their ID). However, with this much items even the StringBuilder will run into performance problems, not to mention SharePoint.

Your best bet is to delete the site and re-create it like pmartin suggested it.


Here is the way we can do it simply:

XmlNode listItems = SharepointUtilities.GetListItems(sharePointCredentials, sharePointCredentials.SharePointListName);
        var nodes = (from nd in listItems.ChildNodes.OfType<XmlNode>() where nd.NodeType.Equals(System.Xml.XmlNodeType.Element) select nd);
        var itemIDs = from nd in nodes.FirstOrDefault().ChildNodes.OfType<XmlNode>() where nd.NodeType.Equals(System.Xml.XmlNodeType.Element) select nd.Attributes["ows_ID"].Value;
        if (itemIDs.Count() > 0)
        {
            Lists spSrv = GetSharepointListsService(sharePointCredentials);
            XmlDocument doc = new XmlDocument();
            XmlElement batchElement = doc.CreateElement("Batch");
            batchElement.SetAttribute("OnError", "Continue");
            string batchCommand = "<Method ID='1' Cmd='Delete'><Field Name='ID'>";
            batchCommand += string.Join("</Field></Method><Method ID='1' Cmd='Delete'><Field Name='ID'>", itemIDs.ToArray());
            batchCommand += "</Field></Method>";
            batchElement.InnerXml = batchCommand;
            spSrv.UpdateListItems(sharePointCredentials.SharePointListName, batchElement);

        }


Open the list in DataSheetView, select all records (ctrl+A) and Delete all records. This is the shortest solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消