How to check sub site exists using .Net managed client object model in SharePoint Online or SharePoint 2013?

SharePoint client object model to check if sub site exists

In this post, we will discuss how we can check if subsite exists inside a site using.Net managed client object model in SharePoint online or SharePoint 2013. Here we have taken a console application.

Open 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 Micsoft.SharePoint.Client.Online.CSOM and then click on Install button like below:

SharePoint client object model to check if sub site exists

SharePoint client object model to check if sub site existsAlso you can read:
Steps to create Azure WebJobs for SharePoint online and deploy in Microsoft Azure

SharePoint Online Site Workflow and Initiation Form Parameters example using SharePoint designer workflow

Rename SharePoint 2013 list column title in list view using jQuery

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 sub site exists else it will return false.

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About 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

View all posts by Bijay Kumar →