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

Need Help Choosing a New GPU After My 4090 Went Bad

Hey everyone, I'm in a bit of a situation. My Nvidia 4090 has developed some VRAM issues and unfortunately, it's out of warranty, so...

Looking for Python Practice Problems to Boost My Skills

I've been learning Python and am finally getting the hang of the basics like loops, functions, and lists. However, I'm feeling trapped in what's...

My WordPress Site Crashed – Need Urgent Help!

I'm a new business owner and pretty clueless about anything web-related. I had a developer from Upwork create a WordPress site for me, which...

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

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

Latest Posts

Latest Questions