0

Is there any way to get all the deleted site collections in SharePoint online using CSOM C# / PNP C# ?. In power shell it is possible, but I am looking in c#.

Thanks in advance...

1

You can use GetDeletedSitePropertiesFromSharePoint from the tenant object.

Try with the below sample code:

string userName = "user@tenant.onmicrosoft.com";
string password = "password";

string siteUrl = "https://tenant-admin.sharepoint.com";

using (ClientContext clientContext = new ClientContext(siteUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
    clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);

    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)
    // { }
}

Reference - Tenant.GetDeletedSitePropertiesFromSharePoint method

  • Thank you @Gautam Sheth. It is returning the deleted sites. – Prakash Apr 11 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.