Steps to delete sites sub sites using .Net managed object model in SharePoint online

In this post we will discuss how to delete sites, sub sites using .Net managed object model in SharePoint online (Download PDF for FREE). Here we have taken a windows application. To work with SharePoint objects we need to add the below details:

– Microsoft.SharePoint.Client
– Microsoft.SharePoint.Client.Runtime
– Microsoft.SharePoint.Client.Taxonomy

SharePoint deveopment training course

Below is the full code:




private void button3_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
string username = ” **********@onlysharepoint2013.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;
}


Read some sharepoint online tutorials:

Hope this will be helpful.

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 →