none
Get all subsites under site collection by Sharepoint 2010 client object model RRS feed

  • Question

  • Hi All,

    My need is to get all sub sites (all sub sites till last level) title, their URLs and owner list in C# console (client model), as we have restriction to run any code to the server.


    arun singh

    Tuesday, September 24, 2013 11:39 AM

Answers

  • Hi,                                                             

    Please take a look at the code from Triad's blog, it retrieves all the subsites' urls and titles:

             static string mainpath = "http://triad102:1001";
             static void Main(string[] args)
             {
                 getSubWebs(mainpath);
                 Console.Read();
             }
             public static  void  getSubWebs(string path)
             {          
                 try
                 {
                     ClientContext clientContext = new ClientContext( path );
                     Web oWebsite = clientContext.Web;
                     clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
                     clientContext.ExecuteQuery();
                     foreach (Web orWebsite in oWebsite.Webs)
                     {
                         string newpath = mainpath + orWebsite.ServerRelativeUrl;
                         getSubWebs(newpath);
                         Console.WriteLine(newpath + "\n" + orWebsite.Title );
                     }
                 }
                 catch (Exception ex)
                 {                
    
                 }           
             }
    

    If you want to get more information of a site, here is a link for your reference:

    Web Class

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web(v=office.14).aspx

    Best regards


    Patrick Liang
    TechNet Community Support

    • Proposed as answer by Sairams Wednesday, September 25, 2013 8:15 AM
    • Marked as answer by Patrick_LiangModerator Sunday, September 29, 2013 4:00 PM
    Wednesday, September 25, 2013 8:03 AM
    Moderator
  • If you want to get the site owners you can ask for the AssociatedOwnerGroup and then query for the users within that group.  The code could look something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using Microsoft.SharePoint.Client;
    
    namespace GetSubSites
    {
        class Program
        {
            private static string websiteURL = "http://yoursite.com";
            
            static void Main(string[] args)
            {
                WriteSubWebs(websiteURL, websiteURL);
                write("");
                write("Press Enter key to exit...");
                Console.ReadLine();
            }
    
            private static void WriteSubWebs(string currentWebURL, string parentWebURL) {
                try{
                    ClientContext clientContext = new ClientContext(currentWebURL);
                    Web web = clientContext.Web;
    
                    clientContext.Load(clientContext.Web);
                    clientContext.Load(web, w => w.Title, w => w.AssociatedOwnerGroup);
    
                    try{
                        clientContext.ExecuteQuery();
                        write("Current Site: " + web.Title);
                        write("URL: " + currentWebURL);
    
                        // Grab owner group and users in this group
                        Group siteOwnersGroup = web.AssociatedOwnerGroup;
                        UserCollection users = siteOwnersGroup.Users;
                        clientContext.Load(siteOwnersGroup.Users);
                        clientContext.ExecuteQuery();
    
                        write("Site Owners Group: " + siteOwnersGroup.Title);
    
                        foreach (var user in users){
                            write("Site Owner User: " + user.Title + "(" + user.LoginName + ")");
                        }
    
                        write(" ");
                        write(" ");
    
                        try{
                            clientContext.Load(web, w => w.Webs);
                            clientContext.ExecuteQuery();
                            var subWebsList = web.Webs;
    
                            if (subWebsList != null && subWebsList.Count > 0){
                                for (var i = 0; i < subWebsList.Count; i++){
                                    var subWeb = subWebsList[i];
                                    string subWebURL = currentWebURL + subWeb.ServerRelativeUrl;
    
                                    WriteSubWebs(subWebURL, currentWebURL);
                                }
                            }
                        }
                        catch (Exception ex) { } // no sub webs, continue
                    }
                    catch (Exception ex){
                        // Write the title of the current sub web, there are no sub webs beneath it
                        write("Current Site URL: " + currentWebURL);
                        write("Inherits all permissions from parent web " + parentWebURL);
                    }
                }
                catch (Exception ex) {  write("error " + ex.Message); }
            }
    
            private static void write(string message){
                Console.WriteLine(message);
            }
        }
    }

    Now, that code will run as you by default.  So if you are logged in as a domain user that has appropriate permissions to SharePoint, then this will work OK.  But, if you are going to run this from anywhere on your network, you're going to have to add an authentication layer.  Visit this page for more information: 

    http://msdn.microsoft.com/en-us/library/ee535729(v=office.14).aspx

    Hope that helps,

    ~ Katie


    Thursday, September 26, 2013 1:43 PM

All replies

  • Hi,                                                             

    Please take a look at the code from Triad's blog, it retrieves all the subsites' urls and titles:

             static string mainpath = "http://triad102:1001";
             static void Main(string[] args)
             {
                 getSubWebs(mainpath);
                 Console.Read();
             }
             public static  void  getSubWebs(string path)
             {          
                 try
                 {
                     ClientContext clientContext = new ClientContext( path );
                     Web oWebsite = clientContext.Web;
                     clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
                     clientContext.ExecuteQuery();
                     foreach (Web orWebsite in oWebsite.Webs)
                     {
                         string newpath = mainpath + orWebsite.ServerRelativeUrl;
                         getSubWebs(newpath);
                         Console.WriteLine(newpath + "\n" + orWebsite.Title );
                     }
                 }
                 catch (Exception ex)
                 {                
    
                 }           
             }
    

    If you want to get more information of a site, here is a link for your reference:

    Web Class

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web(v=office.14).aspx

    Best regards


    Patrick Liang
    TechNet Community Support

    • Proposed as answer by Sairams Wednesday, September 25, 2013 8:15 AM
    • Marked as answer by Patrick_LiangModerator Sunday, September 29, 2013 4:00 PM
    Wednesday, September 25, 2013 8:03 AM
    Moderator
  • If you want to get the site owners you can ask for the AssociatedOwnerGroup and then query for the users within that group.  The code could look something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using Microsoft.SharePoint.Client;
    
    namespace GetSubSites
    {
        class Program
        {
            private static string websiteURL = "http://yoursite.com";
            
            static void Main(string[] args)
            {
                WriteSubWebs(websiteURL, websiteURL);
                write("");
                write("Press Enter key to exit...");
                Console.ReadLine();
            }
    
            private static void WriteSubWebs(string currentWebURL, string parentWebURL) {
                try{
                    ClientContext clientContext = new ClientContext(currentWebURL);
                    Web web = clientContext.Web;
    
                    clientContext.Load(clientContext.Web);
                    clientContext.Load(web, w => w.Title, w => w.AssociatedOwnerGroup);
    
                    try{
                        clientContext.ExecuteQuery();
                        write("Current Site: " + web.Title);
                        write("URL: " + currentWebURL);
    
                        // Grab owner group and users in this group
                        Group siteOwnersGroup = web.AssociatedOwnerGroup;
                        UserCollection users = siteOwnersGroup.Users;
                        clientContext.Load(siteOwnersGroup.Users);
                        clientContext.ExecuteQuery();
    
                        write("Site Owners Group: " + siteOwnersGroup.Title);
    
                        foreach (var user in users){
                            write("Site Owner User: " + user.Title + "(" + user.LoginName + ")");
                        }
    
                        write(" ");
                        write(" ");
    
                        try{
                            clientContext.Load(web, w => w.Webs);
                            clientContext.ExecuteQuery();
                            var subWebsList = web.Webs;
    
                            if (subWebsList != null && subWebsList.Count > 0){
                                for (var i = 0; i < subWebsList.Count; i++){
                                    var subWeb = subWebsList[i];
                                    string subWebURL = currentWebURL + subWeb.ServerRelativeUrl;
    
                                    WriteSubWebs(subWebURL, currentWebURL);
                                }
                            }
                        }
                        catch (Exception ex) { } // no sub webs, continue
                    }
                    catch (Exception ex){
                        // Write the title of the current sub web, there are no sub webs beneath it
                        write("Current Site URL: " + currentWebURL);
                        write("Inherits all permissions from parent web " + parentWebURL);
                    }
                }
                catch (Exception ex) {  write("error " + ex.Message); }
            }
    
            private static void write(string message){
                Console.WriteLine(message);
            }
        }
    }

    Now, that code will run as you by default.  So if you are logged in as a domain user that has appropriate permissions to SharePoint, then this will work OK.  But, if you are going to run this from anywhere on your network, you're going to have to add an authentication layer.  Visit this page for more information: 

    http://msdn.microsoft.com/en-us/library/ee535729(v=office.14).aspx

    Hope that helps,

    ~ Katie


    Thursday, September 26, 2013 1:43 PM
  • Thanks for this hint!!
    Wednesday, August 12, 2015 3:07 PM