question

sravyashivapuram-6930 avatar image
0 Votes"
sravyashivapuram-6930 asked TongZhangMSFT-7548 commented

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

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

office-sharepoint-onlinesharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered TongZhangMSFT-7548 commented

Hi @sravyashivapuram-6930,

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @sravyashivapuram-6930

I am checking to see if the problem has been resolved. If you have any questions or progress, you can contact me in time.

0 Votes 0 ·

Hi @sravyashivapuram-6930,

Have you tried the solution I proposed?

If you have any questions or progress, you can contact me in time.

Looking forward to your reply.

0 Votes 0 ·