We recently had a client ask us to restore a  SharePoint Online site they had deleted.  The site had been deleted about 30 days before, and we knew the day of the deletion.   When we went to the recycle bin and looked for the site, we didn’t see it; even when looking in the second-stage recycle bin. 

Because of the volume of items the client had deleted, we created an application to iterate through the recycle bin and locate the site based on the name of the site and the user who deleted it. 

The code below also handles MFA authentication for the account used to run the application.  Hopefully you will find this helpful if you ever run into the same issue. 

_____________________________________________________________________________________ 

using Microsoft.SharePoint.Client; 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.Threading.Tasks; 

 

namespace GetRecycleBinContents 

{ 

    class Program 

    { 

        static void GetItemsSPOSiteRecycleBin(string sSiteUrl) 

    { 

       //Try Catch block for error handling 

        try 

        { 

            Console.WriteLine(“—————————————————————————-“); 

            Console.WriteLine(“Getting all items in the Recycle Bin of a SPO Site”); 

            Console.WriteLine(“—————————————————————————-“); 

 

 

             //#Special Class to authenticate to SharePoint online with MFA.  Will pop open a window similar to browser for entering username and password and allow for interacting with MFA type logins.  

            var authenticationManager = 

                   new OfficeDevPnP.Core.AuthenticationManager(); 

 

            //# Connect to site 

            ClientContext context = 

            authenticationManager.GetWebLoginClientContext(sSiteUrl, null); 

 

            //#Get web object 

            Web web = context.Web; 

            User user = web.CurrentUser; 

            context.Load(web); 

            context.Load(user); 

            context.ExecuteQuery(); 

            var spoWeb = web; 

 

            //#Get Contents of Site Collection recycle bin 

            var spoRecycleBinItemCollection = context.Site.RecycleBin; 

            context.Load(spoRecycleBinItemCollection); 

            context.ExecuteQuery(); 

 

            //#Loop through recycle bin items 

            foreach (var spoRecycleBinItem in spoRecycleBinItemCollection) 

            { 

                    //# If url contains SITE NAME and was deleted by USER, in the code example, the name of the site is “wiki” and the user is “John” 

                    if (spoRecycleBinItem.LeafName.ToLower().IndexOf(“wiki”) >= 0 && spoRecycleBinItem.DeletedByName.ToLower().IndexOf(John)  >= 0) 

                    { 

                       //Restore the item to original location 

                        spoRecycleBinItem.Restore(); 

                        context.ExecuteQuery(); 

 

                       //#Write out to console the name of the item 

                        Console.WriteLine(“Item:” + spoRecycleBinItem.Title + “- Deleted By:” + spoRecycleBinItem.DeletedByName + “- Deleted Date:” + spoRecycleBinItem.DeletedDate); 

 

                    } 

            } 

 

            //#Destroy the item to clear up memory 

            context.Dispose(); 

        } 

        catch (Exception ex) 

        { 

      Console.WriteLine(ex.Message); 

        } 

    } 

    

       //Starting procedure for app 

        static void  Main(string[] args) 

        { 

            //#Site Collection url, insert your Site Collection URL 

            var sSiteUrl = https://demo.sharepoint.com; 

 

        

           //Call GetItemsSPOSiteRecylceBin procedure passing the parameters for sSiteURL 

            GetItemsSPOSiteRecycleBin(sSiteUrl); 

        } 

    } 

} 

SharePoint is an incredibly flexible platform that can solve many different challenges facing your organization—and improve the efficiency of your IT department. Our SharePoint architects will work with you to design a solution to meet your specific goals, implement that solution for you, and work with your team on user training and adoption planning.  Reach out to us at info@peters.com to connect and learn more – we are happy to help!