Restore Recycle Bin Items From SharePoint Site Using CSOM
Softree Technology > Blogs > SharePoint > Restore Recycle Bin Items From SharePoint Site Using CSOM

Restore Recycle Bin Items From SharePoint Site Using CSOM

  • Posted by: Softree Technology

What is Recycle Bin?

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 stay 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 below steps we can restore all items present in Recycle Bin section.

Steps:

  1. Retrieve Recycle Bin Items from site.
  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 ="testusera@demo.onmicrosoft.com";
             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();

                //Restore all items from Recycle Bin
                recycleBinItems.RestoreAll();
                    
                ctx.ExecuteQuery();
                Console.WriteLine("item restored");
                
             }
          }
           catch (Exception ex) { }
            Console.ReadLine();
        }
    }
}
  1. Before execution of program.

2. After execution of program.

Conclusion:
From the above article we have learnt how to restore items from Recycle Bin programmatically with help of CSOM.

Keywords:
• Restore SharePoint Recycle bin items
• Restore Recycle bin using CSOM
• Retrieve and Restore SharePoint Recycle bin items

Author: Softree Technology

Leave a Reply