Restore Recycle Bin Items From SharePoint Site Using CSOM

Introduction

 
A Recycle Bin is a temporary storage location for deleted items from that site.
 
When we delete an item first it goes to First Stage Recycle Bin and stays there for a short-term period then it moves to second-stage Recycle Bin.
 
In case of permanent deletion, we need to delete it again from second-stage Recycle Bin.
 
Sometimes we may require this item again in that case simply we need to restore it back from Recycle Bin. It can be restored by both manually and programmatically.
 
Here, I am going to explain how to Restore Recycle Bin Items from a SharePoint Site using Client Side Object Model (CSOM).
 
By using the below steps we can restore all items present in the Recycle Bin section.
 
Step 1
 
Retrieve Recycle Bin Items from the site.
 
Step 2
 
Restore it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using System.Security;  
  8. namespace RestoreRecyclebinSharepointItem  
  9. {  
  10.    
  11.    class Program  
  12.    {  
  13.    
  14.       static void Main(string[] args)  
  15.       {  
  16.    
  17.          SecureString pwd = new SecureString();  
  18.          string Username ="[email protected]";  
  19.          string password = "Password";  
  20.       try  
  21.       {  
  22.    using (ClientContext ctx = new ClientContext("https://portal.sharepoint.com/sites/Demosite"))  
  23.          {  
  24.          foreach (char c in password.ToArray())  
  25.             pwd.AppendChar(c);  
  26.          ctx.Credentials = new SharePointOnlineCredentials(Username, pwd);  
  27.          RecycleBinItemCollection recycleBinItems = ctx.Site.RecycleBin;  
  28.          ctx.Load(recycleBinItems);  
  29.          ctx.ExecuteQuery();  
  30.          //Restore all items from Recycle Bin  
  31.          recycleBinItems.RestoreAll();  
  32.    
  33.          ctx.ExecuteQuery();  
  34.          Console.WriteLine("item restored");  
  35.        }  
  36.       }  
  37.       catch (Exception ex) { }  
  38.       Console.ReadLine();  
  39.      }  
  40.    }  
  41. }  
Before the execution of the program.
 
Restore Recycle Bin Items From SharePoint Site Using CSOM
 
After the execution of the program.
 
Restore Recycle Bin Items From SharePoint Site Using CSOM 
 

Conclusion

 
From the above article, we have learned how to restore items from Recycle Bin programmatically with the help of CSOM.