How To Check Existence Of A Subsite And Provision Subsite In SharePoint Online Using CSOM – C# Programmatically

Ahamed Fazil Buhari
 
Senior Developer
March 25, 2019
 
Rate this article
 
Views
3240

Hi,

In the below article we will see how to provision Sub Sites in SharePoint Online. As a first step, we need to check whether the subsite is already exists or not. If it doesn’t exits then we will provision the site using CSOM (Console Application).

I have the Site URL and Client ID values in app.config file. Please refer this article if you are not familiar with Token based authentication

 

The below code with comments explains everything.

 using Microsoft.SharePoint.Client;
 using System;
 
 namespace SpPals.TemplateProvision
 {
     public static class SPpals
     {
         public static void ProvisionFunction()
         {
             string topRootSite = "https://fazildev.sharepoint.com/sites/sppals/";
             var siteUri = new Uri(topRootSite);
             var realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
             // Client ID and Secrent Key will be taken from App.config file
             var accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
             siteUri.Authority, realm).AccessToken;
             using (var ctx = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
             {
                 string subSite = "test2";
                 ProvisionSubSite(subSite, ctx);
             }
         }
 
         static void ProvisionSubSite(string subSite, ClientContext ctx)
         {
             try
             {
                 // If the site is subsite of a subsite, then use OpenWeb as show below in comment
                 // ctx.Site.OpenWeb("top-subsite-url-name").WebExists(subSite);
                 // Check If Subsite existing
                 if (ctx.Site.RootWeb.WebExists(subSite))
                 {
                     Console.WriteLine("Already Exists");
                 }
                 else
                 {
                     WebCreationInformation webCreationInformation = new WebCreationInformation()
                     {
                         // I create TeamSite as subsite - you can find different type of sites here
                         //  https://www.jasjitchopra.com/sharepoint-2013-site-templates-codes-for-powershell/
                         WebTemplate = "STS#0",
                         Title = subSite,
                         Url = subSite,
                         Language = 1033,
                         UseSamePermissionsAsParentSite = true
                     };
                     Web newWeb = ctx.Web.Webs.Add(webCreationInformation);
                     ctx.Load(newWeb);
                     ctx.ExecuteQuery();
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine("Something went wrong. " + ex.Message);
             }
         }
     }
 }
 

Please make sure the following reference from Nuget package are already added in your project and include TokenHelper.cs file into your project for authentication.

 

Happy Coding

Ahamed

Category : Office 365, SharePoint

Author Info

Ahamed Fazil Buhari
 
Senior Developer
 
Rate this article
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint, Azure, M365, SPFx, .NET and client side scripting - JavaScript, TypeScript, ...read more
 

Leave a comment