I wants to retrieve all the sites in sharepoint using domain url(for ex:https://domainname.sharepoint.com). If am giving any site url am getting the sub sites under that site url. But I want all the sites using Domian ulr.Please help.

1 Answer 1


If I have understood correctly, you want to retrieve all the site collections in the tenant using CSOM. If that's the case, you can use the "tenant" object:

DISCLAIMER: Following code has not been tested. Please modify where corrections are required.

    using Microsoft.Online.SharePoint.TenantAdministration;
    using Microsoft.SharePoint.Client;

        SPOSitePropertiesEnumerable prop = null;

        //Pass on credentials
        var password = new SecureString();
        foreach (char c in "MyPassword".ToCharArray()) password.AppendChar(c);
        string userName = "[email protected]";
        var credentials = new SharePointOnlineCredentials(userName, password);

        // ctx is the ClientContext object
        var ctx = new ClientContext("https://MyTenant-admin.sharepoint.com/");
        ctx.Credentials = credentials;

        // Load all site collections from tenant
        Tenant tenant = new Tenant(ctx);
        prop = tenant.GetSiteProperties(0, true);
        foreach (SiteProperties sp in prop)
           Console.WriteLine(sp.Title + " => " + sp.Url);

           // Recursive function to get all sub sites for each site collection
            GetAllSubWebs(sp.Url, userName, password);

        private static void GetAllSubWebs(string path, string userName, SecureString password)  
            // ClienContext - Get the context for the Site collection.              
            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  

                // 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                           
                    string newpath = subWeb.Url;  
                    GetAllSubWebs(newpath, userName, password);  
                    Console.WriteLine(subWeb.Title + "-------" + subWeb.Url);                            
  • I used this one also. but this code will gives site collection of admin.I want all sites for particular domain given by user.In the above code every time am getting SP.Title as empty. Jul 18, 2018 at 5:54
  • If you need all the sites in a particular domain or tenant, you need to access the admin site of that tenant. The admin site has the list of all site collections for that domain. In order to access it, you need to be an O365 admin as well. Are you a tenant admin? And are you passing the correct credentials?
    – Deepu Nair
    Jul 18, 2018 at 5:58
  • yes admin only am passing correct credential also. From sp.Url am getting all the admin site url. How i will get sites inside that one? Jul 18, 2018 at 6:00
  • I have edited the code and added an additional function to loop through the subsites in a site collection recursively.
    – Deepu Nair
    Jul 18, 2018 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.