Saturday, January 25, 2014

Delete All Items from List or Library Programmatically

At times, we may have to delete all items from SharePoint list or library. We can get it done by going to "Manage Content and Structure" option as in my another article How to Remove All responses from SharePoint Survey 

There may be situations, where we've to delete all items from SharePoint list programmatically. Here are the code snippets to achieve the same.

Delete All Items from SharePoint List or Library using PowerShell:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
 
<# for MOSS 2007 Compatibility use:
$site = new-object Microsoft.SharePoint.SPSite($URL)
$web = $site.OpenWeb() 
#>
 
#Get the web object
$web = Get-SPWeb $url
 
#Get the List
$list = $web.Lists["Tasks"]
 
#Get All List Items
$listItems = $list.Items
 
# Delete All List Items - This will delete all items - including items from sub-folders.
$listItems | ForEach { $list.GetItemById($_.Id).Delete()  }  #ForEach can be replaced with %
 
<# Traditional Way of deleting Items from end to avoid: "Collection was modified" Error
for ($i=$listItems.Count-1;$i -ge 0; $i-–)
{
Write-Host("Deleted: " + $listItems[$i].name)
 $listItems[$i].Delete()
}
#>

Filter and Delete List Items:
We can filter the list items using CAML and perform the same delete operation. Here it goes:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
 
$web = Get-SPWeb $url
 
#Get the List
$list = $web.Lists["Tasks"]
 
#CAML Filter Query
$CAMLQuery="<Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>Completed</Value></Eq></Where>"
 
$SPQuery=New-Object Microsoft.SharePoint.SPQuery
$SPQuery.ViewAttributes = "Scope='Recursive'"  #Get all items from Folders also!
$SPQuery.Query=$CAMLQuery
$SPQuery.RowLimit  = 10000;
 
#Get the List items based on Filter
$result=$list.GetItems($SPQuery)
 
#Delete the items from list
$result | % { $list.GetItemById($_.Id).Delete()  }
Delete All Items from SharePoint list Programmatically using C# Object Model:
Same code can be re-written in C# to delete list items programmatically.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using (SPSite oSPsite = new SPSite("http://sharepoint"))
    {
         using (SPWeb oSPWeb = oSPsite.OpenWeb())
         {
             oSPWeb.AllowUnsafeUpdates = true;
 
             // Get the List
             SPList oSPList = oSPWeb.Lists["Tasks"];
 
             for (int i= oSPList.ItemCount-1; i >=0; i--)
             {
                 oSPList.Items[i].Delete();
             }
 
             oSPWeb.AllowUnsafeUpdates = false;
 
         }
     }


Bulk Delete using ProcessBatchData: 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using (SPSite oSPsite = new SPSite("http://sharepoint"))
            {
                 using (SPWeb oSPWeb = oSPsite.OpenWeb())
                 {
                     // Get the List
                     SPList oSPList = oSPWeb.Lists["Tasks"];
 
                     StringBuilder batchString = new StringBuilder();
                     batchString.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
 
                     foreach (SPListItem item in oSPList.Items)
                     {
                         batchString.Append("<Method>");
                         batchString.Append("<SetList Scope=\"Request\">" + Convert.ToString(item.ParentList.ID) + "</SetList>");
                         batchString.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
                         batchString.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
                         batchString.Append("</Method>");
                     }
 
                     batchString.Append("</Batch>");
                     oSPWeb.ProcessBatchData(batchString.ToString());
 
                 }
             }

Delete Vs Recycle: Will the Deleted Items goes to Recycle bin? 
No! Delete method doesn't send files to Recycle bin. But the Recycle Method does! So, call the Recycle method , if you want the deleted items to be sent to Recycle bin.

?
1
2
3
4
5
6
using (SPWeb web = siteColl.AllWebs["Sales"])
{
  SPList list = web.Lists["Tasks"];
  SPListItem item = list.Items[GUID];
  item.Recycle();
}

Reference:

http://www.sharepointdiary.com/2012/11/delete-all-items-from-list-or-library.html

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...