Delete First N Records From SharePoint Online List

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

First here I am getting the total records in the list, then if the count is greater then 4990 then I am getting the first 100 records, then looping each record for deletion. So I will not get list threshhold problem for that view. Here I am using Sharepoint client object model to perform this operations for Sharepoint online.

I think this same code will work for onpremise also. Sharepoint online is more secure for login from client object model. So for logging into Sharepoint online it will not accept direct password. We need to convert our password as secure string, only then can we connect to Sharepoint online and we can work. 

So here I am using GetPasswordFromConsoleInput() function for converting my login password to convert as secure string and then am passing this secure string to SharePointOnlineCredentials() method. Here I used console application to do this task. 
 
So to work with CSOM we need to install Sharepoint client in our system. 
We need to add reference dlls in our console application. 
We need to add sharepoint.client.dll and sharepoint.client.runtime.dll files.

My code
  1. //Main function  
  2. static void Main(string[] args) {  
  3.   
  4.     string webUrl = "https://sharepoint.com/usa/";  
  5.     string userName = "******";  
  6.     string pwd = "*******";  
  7.     SecureString password = GetPasswordFromConsoleInput(pwd);  
  8.   
  9.     using(var context = new ClientContext(webUrl)) {  
  10.         context.Credentials = new SharePointOnlineCredentials(userName, password);  
  11.         context.Load(context.Web, w => w.Title);  
  12.         context.ExecuteQuery();  
  13.         List listName = context.Web.Lists.GetByTitle("MyList");  
  14.         context.Load(listName);  
  15.         context.ExecuteQuery();  
  16.  
  17.  
  18.         #  
  19.         check the count of the sharepoint list  
  20.   
  21.         if (listName.ItemCount > 4990) {  
  22.             CamlQuery query1 = new CamlQuery();  
  23.  
  24.             # in below code am passing 100 as rowlimit means it will get first 100 records from the sharepoint list  
  25.   
  26.             query1.ViewXml = "<View><RowLimit>100</RowLimit></View>";  
  27.             ListItemCollection items = listName.GetItems(query1);  
  28.             context.Load(items);  
  29.             context.ExecuteQuery();  
  30.             if (items.Count > 0) {  
  31.                 for (int i = items.Count - 1; i >= 0; i--) {  
  32.                     items[i].DeleteObject();  
  33.                 }  
  34.                 context.ExecuteQuery();  
  35.             }  
  36.         }  
  37.     }  
  38. }  
  39.   
  40. //this method will Convert password as securestring method.  
  41.   
  42. private static SecureString GetPasswordFromConsoleInput(string password) {  
  43.   
  44.     //ConsoleKeyInfo info;   
  45.     //Get the user's password as a SecureString  
  46.   
  47.     SecureString securePassword = new SecureString();  
  48.     foreach(char c in password) {  
  49.         securePassword.AppendChar(c);  
  50.     }  
  51.     return securePassword;  
  52. }  
Ebook Download
View all
Learn
View all