RecycleBin Items Using CSOM

  1. //CSOM Assembly version - 16.1.4727.1200  
  2. //using Microsoft.SharePoint.Client  
  3.   
  4. //GetFirstStageRecyclebinItems(credentials, weburl);  
  5.   
  6.   private static void GetFirstStageRecyclebinItems(SharePointOnlineCredentials credentials, string weburl)  
  7.   {  
  8.       RecycleBinItemCollection recyclebinItems = null;  
  9.       using (ClientContext ctx = new ClientContext(weburl))  
  10.       {  
  11.           ctx.Credentials = credentials;  
  12.           Web oweb = ctx.Web;  
  13.           //SP.Web.GetRecycleBinItems : retrieves the deleted items from the web site.  
  14.           //Row Limit - 100  : Returns deleted items upto 100  
  15.           //RecycleBinOrderBy.DefaultOrderBy - Order the items by default  
  16.           //RecycleBinItemState.FirstStageRecycleBin - returns the items only from first stage  
  17.           recyclebinItems = oweb.GetRecycleBinItems(null,100, false, RecycleBinOrderBy.DefaultOrderBy, RecycleBinItemState.FirstStageRecycleBin);  
  18.           ctx.Load(recyclebinItems);  
  19.           ctx.ExecuteQuery();  
  20.       }  
  21.       Console.WriteLine("Total deleted items:" + recyclebinItems.Count.ToString());  
  22.       //RecycleBinItem.ItemType - Returns the type of the deleted object  
  23.       //RecycleBinItem.LeafName - Returns the item name / file name of the object  
  24.       foreach (RecycleBinItem item in recyclebinItems)              
  25.           Console.WriteLine(item.ItemType + " - " + item.LeafName);              
  26.   
  27.       Console.WriteLine("Press any key to exit...");  
  28.       Console.Read();  
  29.   }