Create subsite in SharePoint programmatically

This SharePoint tutorial explains, how to create a SharePoint subsite programmatically using the server object model and client object model in SharePoint 2013/2016.

Also, we will see how to delete a SharePoint subsite programmatically using the SharePoint client object model (CSOM).

I have used a windows application to create subsites in SharePoint 2016. But the same code will work for SharePoint 2013.

Create subsite in SharePoint programmatically using server object model

Below is the code to create a subsite using SharePoint 2016 server object model.

using (SPSite site = new SPSite("http://mypc:29024/sites/SPTraining/"))
{
using (SPWeb newWeb = site.OpenWeb())
{
newWeb.AllowUnsafeUpdates = true;
SPWebCollection subsites = newWeb.Webs;
SPWeb newSubWeb = subsites.Add("MySiteFromServerObjectModel", "My Site from Server Object Model", "This is a site from server object model.", 1033, "STS#0", true, false);
newWeb.Update();
}
}

Create subsite in SharePoint programmatically using csom (client side object model)

Below is the csom code to create a subsite programmatically using csom in SharePoint 2013/2016.

using (ClientContext ctx = new ClientContext("http://mypc:29024/sites/SPTraining/"))
{
WebCreationInformation websitecreationinfo = new WebCreationInformation();
websitecreationinfo.Url = "MySiteFromClientObjectModel";
websitecreationinfo.Title = "My Site from Client Object Model";
websitecreationinfo.Description = "This is a site from client object model.";
websitecreationinfo.UseSamePermissionsAsParentSite = true;
websitecreationinfo.WebTemplate = "STS#0";
websitecreationinfo.Language = 1033;
Web w = ctx.Site.RootWeb.Webs.Add(websitecreationinfo);
ctx.ExecuteQuery();
}
Create subsite in SharePoint programmatically

Delete sites and subsites programmatically using CSOM in SharePoint Online

Let us see 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;
}

By using the above code, we can delete all SharePoint sites and subsite programmatically.

You may like following SharePoint object model tutorials:

In this SharePoint tutorial, we learned how to create a subsite programmatically in SharePoint using server object model or client object model (CSOM). Also, we learned how to delete a subsite or site in SharePoint programmatically.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>