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. // Assembly Reference Used: OfficeDevPnP.Core, Version=2.4.1605.0, Culture=neutral, PublicKeyToken=3751622786b357c2
  9. namespace SampleApplication
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. string siteUrl = "https://sharepointonline.sharepoint.com";
  16. AuthenticationManager authManager = new AuthenticationManager();
  17. //Interactive Login to SharePoint site - Opens a Online signin page to authenticate the user
  18. var context = authManager.GetWebLoginClientContext(siteUrl);
  19. IEnumerable<string> webUrls= context.Site.GetAllWebUrls();
  20. string weburl = "";
  21. foreach(var url in webUrls)
  22. {
  23. weburl += url + "\r\n";
  24. }
  25. Console.WriteLine(weburl);
  26. Console.WriteLine("Press any key to exit.");
  27. Console.ReadKey();
  28. }
  29. }
  30. }