SharePoint Check if subsite exists using CSOM

This SharePoint tutorial explains, how to check if subsite exists using csom. We will use .Net managed client object model (csom) in SharePoint Online to check if subsite exists sharepoint c#.

Here we will create a console application to do the demo on the check is subsite exists sharepoint csom.

You can also check how to Create subsite in SharePoint 2013 Online programmatically using CSOM and how to get all subsites from a site collection in SharePoint Online using CSOM.

Check is subsite exists SharePoint csom

Now, we will write our csom code to check if the subsite exists in SharePoint Online using c#.

Open visual studio and then click on File -> New -> Project…

And then choose Choose Visual C# -> Windows and then choose Console Application from the list of templates.

Now we can add Microsoft.SharePoint.Client.dll from NuGet Package.

Then go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution…

And then search for Microsoft.sharepoint.client and then select Microsoft.SharePoint.Client.Online.CSOM and then click on Install button like below:

check if subsite exists sharepoint c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;

namespace ConsoleApplication1
class Program
static void Main(string[] args)
bool isExists;
var password = new SecureString();
foreach (char c in "*********".ToCharArray()) password.AppendChar(c);
var credentials = new SharePointOnlineCredentials("******", password);

using (ClientContext context = new ClientContext(""))
context.Credentials = credentials;
var web = context.Web;
context.Load(web, w => w.Webs);
var subWeb = (from w in web.Webs where w.Url == "" select w).SingleOrDefault();
if (subWeb != null)
isExists = true;
isExists = false;

The above code will return true if the SharePoint subsite exists else it will return false.

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to check if subsite exists in SharePoint using CSOM.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

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