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("******@OnlySharePoint2013.onmicrosoft.com", password);

using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
context.Credentials = credentials;
var web = context.Web;
context.Load(web, w => w.Webs);
context.ExecuteQuery();
var subWeb = (from w in web.Webs where w.Url == "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySubSite" select w).SingleOrDefault();
if (subWeb != null)
{
isExists = true;
}
else
{
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.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>