Get all website URLs from SharePoint site using CSOM (PnP Core Component)

This blog contains the codesnippet 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.

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

Method:

IEnumerable<string> GetAllWebUrls()

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

[code lang=”csharp”]
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(weburl);

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

[/code]
The above code also available from GitHub.

Shantha Kumar
Shantha Kumar
Articles: 280

24,849 Comments

Comments are closed.