Delete First N Records From SharePoint List Online Using Power Shell

In today's world, PowerShell is a main concept for every SharePoint developer. Using PowerShell, we can do SharePoint tasks easily and very fast. So, in this article, I am sharing on of my experience task. I want to delete first n records from the SharePoint list (Office 365).

Here, I have a custom list with few views. In particular view display, I am getting list threshold problem. Because in that view, am displaying total records from the list, so my requirements is that when the items in list is crossing 4990, then immediately I need to delete first 100 records from the list.

First, I am getting the total records in the list. Then, if the count is greater than 4990, I am getting first 100 records. Then, I am looping each record for deletion. So, I will not get list threshold problem for my view. Here, I am using SharePoint client object model to perform this operation for SharePoint online. I think this same code will work for on premises also.
 
So, for logging into SharePoint online, it will not accept direct password. We need to convert our password as secure string.

My code
  1. #  
  2. first we need to include the sharepoint client dll and client runtime dll 's  
  3.   
  4. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
  5. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  6. $0 = $MyInvocation.MyCommand.Definition  
  7. $dp0 = [System.IO.Path]::GetDirectoryName($0)  
  8.   
  9. $url = "https://sharepoint.com/usa/"  
  10. $username = "Username"  
  11. $password = "*******"  
  12.  
  13. # in below line am converting my sharepoint online password to secure string.  
  14.   
  15. $securePassword = ConvertTo - SecureString $password - AsPlainText - Force# connect / authenticate to sharepoint online and get ClientContext object...  
  16. $clientContext = New - Object Microsoft.SharePoint.Client.ClientContext($url)  
  17. $credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)  
  18. $clientContext.Credentials = $credentials  
  19.   
  20. if (!$clientContext.ServerObjectIsNull.Value) {  
  21.     $web = $clientContext.Site.RootWeb  
  22.     $clientContext.Load($web)  
  23.     $clientContext.ExecuteQuery()  
  24.   
  25.     $list = $clientContext.Web.Lists.GetByTitle('MyList')  
  26.   
  27.   
  28.     $clientContext.Load($list)  
  29.     $clientContext.ExecuteQuery()  
  30.   
  31.   
  32.     $query = New - Object Microsoft.SharePoint.Client.CamlQuery  
  33.     $query.ViewXml = "<View><RowLimit>5000</RowLimit></View>"  
  34.     $items = $list.GetItems($query)  
  35.     $clientContext.Load($items)  
  36.     $clientContext.ExecuteQuery()  
  37.     Write - Host "Total number of ListItems: "  
  38.     $items.Count  
  39.  
  40.     # check the count of the sharepoint list  
  41.   
  42.     if ($items.Count - gt 4990) {  
  43.         $query1 = New - Object Microsoft.SharePoint.Client.CamlQuery  
  44.  
  45.         # in below code am passing 100 as rowlimit means it will get first 100 records from the sharepoint list  
  46.   
  47.         $query1.ViewXml = "<View><RowLimit>100</RowLimit></View>"  
  48.         $items1 = $list.GetItems($query1)  
  49.         $clientContext.Load($items1)  
  50.         $clientContext.ExecuteQuery()  
  51.         if ($items1.Count - gt 0) {  
  52.             for ($i = $items1.Count - 1; $i - ge 0; $i--) {  
  53.                 $items1[$i].DeleteObject()  
  54.             }  
  55.             $clientContext.ExecuteQuery()  
  56.         }  
  57.     }  
  58. }  
Happy coding.