Get all sites and subsites in SharePoint Online using C#

We can easily get the list of all sites and sub sites of all site collections from a Office 365 tenant using SharePoint’s client object model (CSOM) in C#. In this article, I am going to write C# code sample to retrieve all sites and sub sites in a SharePoint tenant and get all subsites for the given site url.

Before proceed, you need to download and install the SharePoint Server 2013 Client Components SDK and add the following dlls in your project reference.

– Microsoft.Online.SharePoint.Client.Tenant.dll
– Microsoft.SharePoint.Client.dll
– Microsoft.SharePoint.Client.Runtime.dll

The below C# code lists all sites and sub sites (nested sub sites) for all sites collections in the given SharePoint tenant. You have to provide your own tenant url, admin username and password.

private static void Get_AllSites_and_SubSites()
{
    SPOSitePropertiesEnumerable prop = null;

    var password = new SecureString();
    foreach (char c in "MyPassword".ToCharArray()) password.AppendChar(c);
    var credentials = new SharePointOnlineCredentials("[email protected]", password);
    var ctx = new ClientContext("https://MyTenant-admin.sharepoint.com/");
    ctx.Credentials = credentials;

    Tenant tenant = new Tenant(ctx);
    prop = tenant.GetSiteProperties(0, true);
    ctx.Load(prop);
    ctx.ExecuteQuery();
    //#1 . Getting all site collections
    foreach (SiteProperties sp in prop)
    {
        Console.WriteLine("Site collection: " + sp.Title + " => " + sp.Url);
        Console.WriteLine("------------------------------------------------");
        var siteCtx = new ClientContext(sp.Url);
        siteCtx.Credentials = credentials;
        var web = siteCtx.Web;
        List allSites = new List();
        siteCtx.Load(web, w=>w.Webs, w => w.Title, w => w.Url);
        siteCtx.ExecuteQuery();
        //#2 . Adding first level site of the site collection
        allSites.Add(web);
        //#3 . Getting sub sites and all the nested sub sites
        GetSubSites(siteCtx, web.Webs, ref allSites);
        foreach (Web site in allSites)
        {
            Console.WriteLine("Site : " + site.Title + " => " + site.Url);
            Console.WriteLine("-------------------");
        }
    }
}

private static void GetSubSites(ClientContext siteCtx, WebCollection webs, ref List allSites)
{
    if (webs.Count > 0)
    {
        siteCtx.Load(webs, w => w.Include(a => a.Webs, a => a.Title, a => a.Url));
        siteCtx.ExecuteQuery();
        foreach (Web web in webs)
        {
            allSites.Add(web);
            GetSubSites(siteCtx, web.Webs, ref allSites);
        }
        siteCtx.ExecuteQuery();
    }
}

Get all sites and subsites for the given site url

The below C# CSOM code lists all sites and sub sites (nested sub sites) for the given SharePoint Online site url.

private static void Get_AllSites_and_SubSites_For_Given_Site(string siteUrl)
{
    var password = new SecureString();
    foreach (char c in "MyPassword".ToCharArray()) password.AppendChar(c);
    var credentials = new SharePointOnlineCredentials("[email protected]", password);

    var siteCtx = new ClientContext(siteUrl);
    siteCtx.Credentials = credentials;
    var web = siteCtx.Web;
    List allSites = new List();
    siteCtx.Load(web, w => w.Webs, w => w.Title, w => w.Url);
    siteCtx.ExecuteQuery();
    allSites.Add(web);
    //Getting sub sites and all the nested sub sites
    GetSubSites(siteCtx, web.Webs, ref allSites);
    foreach (Web site in allSites)
    {
        Console.WriteLine(site.Title + " => " + site.Url);
        Console.WriteLine("-------------------");
    }
}

private static void GetSubSites(ClientContext siteCtx, WebCollection webs, ref List allSites)
{
    if (webs.Count > 0)
    {
        siteCtx.Load(webs, w => w.Include(a => a.Webs, a => a.Title, a => a.Url));
        siteCtx.ExecuteQuery();
        foreach (Web web in webs)
        {
            allSites.Add(web);
            GetSubSites(siteCtx, web.Webs, ref allSites);
        }
        siteCtx.ExecuteQuery();
    }
}

Advertisement

1 thought on “Get all sites and subsites in SharePoint Online using C#”

Leave a Comment