Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Get All Subsites From A Site Collection In SharePoint Online Using CSOM
WhatsApp
Vijai Anand Ramalingam
8y
66
k
0
2
25
Blog
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
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Security;
using
System.Text;
using
System.Threading.Tasks;
using
Microsoft.SharePoint.Client;
namespace
GetAllSubsites
{
class
Program
{
static
void
Main(
string
[] args)
{
string
userName =
"
[email protected]
"
;
string
siteURL =
"https://c986.sharepoint.com/sites/Vijai"
;
Console.WriteLine(
"Enter your password."
);
SecureString password = GetPassword();
GetAllSubWebs(siteURL, userName, password);
Console.ReadLine();
}
private
static
SecureString GetPassword()
{
ConsoleKeyInfo info;
//Get the user's password as a SecureString
SecureString securePassword =
new
SecureString();
do
{
info = Console.ReadKey(
true
);
if
(info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while
(info.Key != ConsoleKey.Enter);
return
securePassword;
}
private
static
void
GetAllSubWebs(
string
path,
string
userName, SecureString password)
{
// ClienContext - Get the context for the SharePoint Online Site
using
(var clientContext =
new
ClientContext(path))
{
// SharePoint Online Credentials
clientContext.Credentials =
new
SharePointOnlineCredentials(userName, password);
// Get the SharePoint web
Web web = clientContext.Web;
clientContext.Load(web, website => website.Webs, website => website.Title);
// Execute the query to the server
clientContext.ExecuteQuery();
// Loop through all the webs
foreach
(Web subWeb
in
web.Webs)
{
// Check whether it is an app URL or not - If not then get into this block
if
(subWeb.Url.Contains(path))
{
string
newpath = subWeb.Url;
GetAllSubWebs(newpath, userName, password);
Console.WriteLine(subWeb.Title +
"-------"
+ subWeb.Url);
}
}
}
}
}
}
Site Collection
SharePoint Online
CSOM
Up Next
How To Get All Site Collection Administrators From SharePoint Online Site Collections Using PnP PowerShell
Ebook Download
View all
Configure MinRole Search and Cloud Hybrid Features in SharePoint Server 2016 and Office 365
Read by 1.6k people
Download Now!
Learn
View all
Membership not found