How to delete SharePoint Site Collection using pnp csom programmatically

蓝咒 提交于 2021-02-11 14:24:07

问题


I have a requirement on SharePoint Online Office 365. As per my requirement, I have to delete all the Site Collection from SharePoint Online Office 365 Admin Center using pnp csom programmatically.

Anyone can suggest that how can I delete all the Site Collection?


回答1:


You can use DeleteSiteCollection extension method to remove the site collection.

It can be used as below:

string userName = "admin@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);

    // use false, if you want to keep site collection in recycle bin
    tenant.DeleteSiteCollection("https://tenant.sharepoint.com/sites/Test", true);

}



回答2:


Connect to SharePoint Online with global administrator account:

Connect-PnPOnline https://tenant-admin.sharepoint.com

Remove specific site collection by url:

Remove-PnPTenantSite -Url   https://tenant.sharepoint.com/sites/sitename-Force -SkipRecycleBin

Remove-PnPTenantSite



来源:https://stackoverflow.com/questions/55722180/how-to-delete-sharepoint-site-collection-using-pnp-csom-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!