How to Get all the Recycle Bin Items Using CSOM in SharePoint 2013 Online

Code Snippet: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Security;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace CSOMOffice365  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.   
  17.             string userName = "[email protected]";  
  18.   
  19.             Console.WriteLine("Enter your password.");  
  20.             SecureString password = GetPassword();  
  21.   
  22.             // ClienContext - Get the context for the SharePoint Online Site  
  23.             // SharePoint site URL - https://c986.sharepoint.com  
  24.   
  25.             using (var clientContext = new ClientContext("https://c986.sharepoint.com"))  
  26.             {  
  27.                 // SharePoint Online Credentials  
  28.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
  29.   
  30.                 // Get the SharePoint web  
  31.                 Web web = clientContext.Web;  
  32.   
  33.                 // Get all the recycle bin items  
  34.                 // Reference: https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.client.recyclebinitem_members.aspx                 
  35.                 RecycleBinItemCollection rbiColl = web.RecycleBin;  
  36.                 clientContext.Load(rbiColl);  
  37.   
  38.                 // Execute the query to the server  
  39.                 clientContext.ExecuteQuery();  
  40.   
  41.                 // Loop through each recycle bin item  
  42.                 foreach (RecycleBinItem rbiItem in rbiColl)  
  43.                 {  
  44.                     Console.WriteLine(rbiItem.Title);  
  45.                 }  
  46.                 Console.ReadLine();  
  47.             }  
  48.         }  
  49.   
  50.         private static SecureString GetPassword()  
  51.         {  
  52.             ConsoleKeyInfo info;  
  53.   
  54.             //Get the user's password as a SecureString  
  55.             SecureString securePassword = new SecureString();  
  56.             do  
  57.             {  
  58.                 info = Console.ReadKey(true);  
  59.                 if (info.Key != ConsoleKey.Enter)  
  60.                 {  
  61.                     securePassword.AppendChar(info.KeyChar);  
  62.                 }  
  63.             }  
  64.             while (info.Key != ConsoleKey.Enter);  
  65.             return securePassword;  
  66.         }  
  67.     }  
  68. } 
Output:  
 
 
Ebook Download
View all
Learn
View all