How to retrieve all deleted sites in SharePoint Online using CSOM?

sravya shivapuram 211 Reputation points
2022-05-04T21:03:12.873+00:00

Hi,

Is there a way to retrieve the list of all deleted sites from a single site collection in SharePoint online using CSOM? All these deleted sites are currently in the second stage recycle bin.

Any help is greatly appreciated. Thank you in advance.

Regards
SLS

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,211 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,789 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,141 Reputation points
    2022-05-05T02:55:24.86+00:00

    Hi @sravya shivapuram ,

    According to my research and testing, you can try to use the following CSOM script to get all deleted sites in SharePoint Online:

            private class Configuration  
            {  
                public static string ServiceSiteUrl = "https://xxxxx.sharepoint.com";  
                public static string ServiceUserName = "xxxx@xxxxxx.onmicrosoft.com";  
                public static string ServicePassword = "xxxxxxx";  
            }  
      
            static ClientContext GetonlineContext()  
            {  
                var securePassword = new SecureString();  
                foreach (char c in Configuration.ServicePassword)  
                {  
                    securePassword.AppendChar(c);  
                }  
                var onlineCredentials = new SharePointOnlineCredentials(Configuration.ServiceUserName, securePassword);  
                var context = new ClientContext(Configuration.ServiceSiteUrl);  
                context.Credentials = onlineCredentials;  
                return context;  
            }  
            static void Main(string[] args)  
            {  
                var clientContext = GetonlineContext();  
                Web web = clientContext.Web;  
                var tenant = new Tenant(clientContext);  
                var deletedSites = tenant.GetDeletedSitePropertiesFromSharePoint("0");  
                clientContext.Load(deletedSites, c => c.IncludeWithDefaultProperties(s => s.Url, s => s.SiteId, s => s.DaysRemaining, s => s.Status));  
                clientContext.ExecuteQueryRetry();  
                // loop through sites   
                foreach(var site in deletedSites)  
                {  
                    Debug.WriteLine(site.Url);  
                }  
            }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. 2023-03-28T07:27:55.9333333+00:00

    Is there a way to do the paging incase of the deleted sites are more than 5000

    0 comments No comments