How to get deleted site collections in SharePoint Online
-
06-02-2021 - |
Question
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...
La solution
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
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange