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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Security;
- namespace RestoreRecyclebinSharepointItem
- {
-
- class Program
- {
-
- static void Main(string[] args)
- {
-
- SecureString pwd = new SecureString();
- string Username ="[email protected]";
- string password = "Password";
- try
- {
- using (ClientContext ctx = new ClientContext("https://portal.sharepoint.com/sites/Demosite"))
- {
- foreach (char c in password.ToArray())
- pwd.AppendChar(c);
- ctx.Credentials = new SharePointOnlineCredentials(Username, pwd);
- RecycleBinItemCollection recycleBinItems = ctx.Site.RecycleBin;
- ctx.Load(recycleBinItems);
- ctx.ExecuteQuery();
-
- recycleBinItems.RestoreAll();
-
- ctx.ExecuteQuery();
- Console.WriteLine("item restored");
- }
- }
- catch (Exception ex) { }
- Console.ReadLine();
- }
- }
- }
Before the execution of the program.
After the execution of the program.
Conclusion
From the above article, we have learned how to restore items from Recycle Bin programmatically with the help of CSOM.