Skip to main content

Get All SubWebs using Client Object Model


  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)
            {
               
            }
         
         
        }

Comments

  1. Thank you so much. This is very helpful.

    ReplyDelete
  2. Can i get the list of all Site Collections in an Web Application using this code? like "View all Sitecollections" in Central Administration

    ReplyDelete

Post a Comment