How To Get All Subsites From A Site Collection In SharePoint Online Using CSOM

In this blog, you will see how to get all the subsites from a site collection in SharePoint Online, using CSOM. Please refer to my previous article Connect To SharePoint 2013 Online Using CSOM With Console Application.
 
Code Snippet
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Security;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.SharePoint.Client;  
  9.   
  10. namespace GetAllSubsites  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string userName = "[email protected]";  
  17.             string siteURL = "https://c986.sharepoint.com/sites/Vijai";  
  18.             Console.WriteLine("Enter your password.");  
  19.             SecureString password = GetPassword();  
  20.             GetAllSubWebs(siteURL, userName, password);  
  21.             Console.ReadLine();  
  22.   
  23.         }  
  24.   
  25.         private static SecureString GetPassword()  
  26.         {  
  27.             ConsoleKeyInfo info;  
  28.             //Get the user's password as a SecureString    
  29.             SecureString securePassword = new SecureString();  
  30.             do  
  31.             {  
  32.                 info = Console.ReadKey(true);  
  33.                 if (info.Key != ConsoleKey.Enter)  
  34.                 {  
  35.                     securePassword.AppendChar(info.KeyChar);  
  36.                 }  
  37.             }  
  38.             while (info.Key != ConsoleKey.Enter);  
  39.             return securePassword;  
  40.         }  
  41.   
  42.         private static void GetAllSubWebs(string path, string userName, SecureString password)  
  43.         {  
  44.             // ClienContext - Get the context for the SharePoint Online Site               
  45.             using (var clientContext = new ClientContext(path))  
  46.             {  
  47.                 // SharePoint Online Credentials    
  48.                 clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
  49.   
  50.                 // Get the SharePoint web  
  51.                 Web web = clientContext.Web;  
  52.                 clientContext.Load(web, website => website.Webs, website => website.Title);  
  53.   
  54.                 // Execute the query to the server  
  55.                 clientContext.ExecuteQuery();  
  56.   
  57.                 // Loop through all the webs  
  58.                 foreach (Web subWeb in web.Webs)  
  59.                 {  
  60.                     // Check whether it is an app URL or not - If not then get into this block  
  61.                     if (subWeb.Url.Contains(path))  
  62.                     {  
  63.                         string newpath = subWeb.Url;  
  64.                         GetAllSubWebs(newpath, userName, password);  
  65.                         Console.WriteLine(subWeb.Title + "-------" + subWeb.Url);  
  66.                     }  
  67.                 }  
  68.             }  
  69.         }  
  70.     }  
  71. }