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


IEnumerable<string> GetAllWebUrls()

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 subsite.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;

// Assembly Reference Used: OfficeDevPnP.Core, Version=2.4.1605.0, Culture=neutral, PublicKeyToken=3751622786b357c2
namespace SampleApplication
    class Program
        static void Main(string[] args)
            string siteUrl = "https://sharepointonline.sharepoint.com";

            AuthenticationManager authManager = new AuthenticationManager();
            //Interactive Login to SharePoint site - Opens a Online signin page to authenticate the user
            var context = authManager.GetWebLoginClientContext(siteUrl);

            IEnumerable<string> webUrls = context.Site.GetAllWebUrls();
            string weburl = "";
            foreach (var url in webUrls)
                weburl += url + "\r\n";

            Console.WriteLine("Press any key to exit.");

The above code also available from GitHub.