Create subsite in SharePoint 2013/2016 programmatically

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

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

You may like following SharePoint object model tutorials:

This SharePoint tutorial, we learned how to create a subsite programmatically using SharePoint 2016/2013 server object model or client object model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

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

>