Get all Website URLs from SharePoint Site using CSOM (PnP Core Component)

WebExtensions.GetAllWebUrls (PnP Core Component) – This extension method returns the collections of the URLs of all web sites that are contained within the site collection including the top-level site and its sub sites.

Supports: SharePoint 2013+, SharePoint Online
 
Assembly: OfficeDevPnP.Core.dll
Namespace: Microsoft.SharePoint.Client

Syntax:

Single Line Code: Site.GetAllWebUrls()

Code Snippet:

The following example returns the collection of all web sites from the Site Collection including top level site and its sub site.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using OfficeDevPnP.Core;  
  8.   
  9. // Assembly Reference Used: OfficeDevPnP.Core, Version=2.4.1605.0, Culture=neutral, PublicKeyToken=3751622786b357c2  
  10. namespace SampleApplication  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             string siteUrl = "https://sharepointonline.sharepoint.com";             
  17.   
  18.             AuthenticationManager authManager = new AuthenticationManager();  
  19.             //Interactive Login to SharePoint site - Opens a Online signin page to authenticate the user  
  20.             var context = authManager.GetWebLoginClientContext(siteUrl);  
  21.   
  22.             IEnumerable<string> webUrls= context.Site.GetAllWebUrls();              
  23.             string weburl = "";  
  24.             foreach(var url in webUrls)  
  25.             {  
  26.                 weburl += url + "\r\n";  
  27.             }  
  28.             Console.WriteLine(weburl);  
  29.   
  30.             Console.WriteLine("Press any key to exit.");  
  31.             Console.ReadKey();  
  32.               
  33.         }   
  34.     }  
  35. }