Delete sites and subsites programmatically using CSOM in SharePoint Online

Delete sites and subsites programmatically using CSOM
SharePoint deveopment training course

This SharePoint CSOM tutorial, we will discuss how to delete all sites, subsites programmatically using csom in SharePoint Online.

Here I am testing it using a windows application, you can also do it using a console application to connect to SharePoint Online sites.

To work with SharePoint objects using csom, we need to add the below dlls.

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

Delete sites and subsites programmatically using CSOM in SharePoint

Below is the full code to delete sites and subsites programmatically from a SharePoint site collection using csom in SharePoint Online.

private void button3_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext(“https://<tenantname>.sharepoint.com/sites/Bhawana/”))
{
string username = ” **********@<tenantname>.onmicrosoft.com”;
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in ” **********”)
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, secureString);
try
{
DeleteSites(context, username, secureString);
}
catch (Exception ex)
{
throw;
}
}
}

public void DeleteSites(ClientContext context, string username, SecureString password)
{
Hashtable hsSites = getAllSites(context, context.Url.ToString(), username, password);
foreach (DictionaryEntry item in hsSites)
{
Hashtable hsSubsites = getSubWebs((Web)item.Key, item.Value.ToString(), username, password);
foreach (DictionaryEntry item1 in hsSubsites)
{
ClientContext ct = new ClientContext(item1.Value.ToString());
ct.AuthenticationMode = ClientAuthenticationMode.Default;
ct.Credentials = new SharePointOnlineCredentials(username, password);
Web web1 = ct.Web;
web1.DeleteObject();
ct.ExecuteQuery();
}
ClientContext ctSite = new ClientContext(item.Value.ToString());
ctSite.AuthenticationMode = ClientAuthenticationMode.Default;
ctSite.Credentials = new SharePointOnlineCredentials(username, password);
Web web = ctSite.Web;
web.DeleteObject();
ctSite.ExecuteQuery();
}
}
public Hashtable getAllSites(ClientContext clientContext, string url, string username, SecureString password)
{
Hashtable hsSites = new Hashtable();
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(username, password);
var Web = clientContext.Web;
clientContext.Load(Web, w => w.Webs);
clientContext.ExecuteQuery();
foreach (Web orWebsite in Web.Webs)
{
hsSites.Add(orWebsite, orWebsite.Url);
}
return hsSites;
}

public Hashtable getSubWebs(Web Website, string siteUrl, string username, SecureString password)
{
Hashtable hsSubSites = new Hashtable();
try
{
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(username, password);
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
clientContext.ExecuteQuery();
foreach (Web orWebsite in oWebsite.Webs)
{
hsSubSites.Add(orWebsite, orWebsite.Url);
getSubWebs(orWebsite, orWebsite.Url, username, password);
}
}
catch (Exception ex)
{
}
return hsSubSites;
}

You may like following SharePoint csom tutorials:

This SharePoint tutorial we discussed how to delete all the sites and subsites programmatically using csom in SharePoint Online.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →