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