Creating A Sharepoint Subsite Using CSOM With C#

This tutorial will show you how to programmatically create a subsite in Sharepoint. This can be done a few different ways and if you have server access you may prefer to do this on the server side, but you can just as easily create one using the Client-Side Object Model.

You can do this using a standard C# console application. For this example I used Visual Studio 2013 with .NET 4.0. You will need to include 2 assemblies in order for the code to work.

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

If this code throws an error then you will need to add the reference to your project. For me, I was not able to find Microsoft.Sharepoint but when I added the .client assembly it worked.

using (ClientContext ctx = new ClientContext("http://site.com"))
{
	WebCreationInformation wci = new WebCreationInformation();
	wci.Url = "mysite" // This url is relative to the url provided in the context
	wci.Title = "My Site";
	wci.Description = description;
	wci.UseSamePermissionsAsParentSite = true;
	wci.WebTemplate = "STS#0";
	wci.Language = 1033; 
	
	Web w = ctx.Site.RootWeb.Webs.Add(wci);
	ctx.ExecuteQuery();
}

Thats it! The code above is all you need to create a new subsite. If you make a mistake and want to change it, you can also do this quite easy using similar code. You can modify pretty much everything on a subsite, but the URL becomes read only so you cannot change this once it has been set.

using (var context = new ClientContext("http://site.com/subsite")
{
	//context.Credentials = credentials;
	var site = context.Web;
	context.Load(site);
	context.ExecuteQuery();
	site.Title = "New Title";
	site.Description = "New Description";
	site.Update();
	context.ExecuteQuery();
}

This is all you need to be able to add a subsite and to be able to edit it.

Related Articles

Related Questions

Feeling Overwhelmed About Learning to Code as a Teen

Hey everyone! I'm 14 and I've been creating some cool games on Scratch for a few years now. However, I'm eager to dive into...

How to Upgrade to Windows 11 Without Automatic Reboots?

Hey everyone! I'm currently rolling out Windows 11 upgrades on eligible machines using PDQ Deploy and the Windows 11 installation assistant. I'm running this...

Why Did My Google Chrome Links Change to a Darker Color?

I've noticed that all the links in my Google Chrome browser suddenly changed to a much darker blue. Initially, I thought they turned black!...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions