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
[Total: 1    Average: 5/5]
 
Views
732

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 Volvo.ProjectSteering.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
[Total: 1    Average: 5/5]
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint 2013, 2016 and O365, Azure, ASP.NET, SQL Server, IIS and client ...read more
 

Leave a comment