locked
subsite's title & lists inside these subsites RRS feed

  • Question

  • Hello everyone I have actually 2 questions; 

    first one is; 

    I want to get all subsites, their urls and titles, (in a specific web scope)

    second one is ;

    I want to get these subsites's all lists. 

    I applied this but returns me null. any suggessions?

    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                 {
                     using (SPWeb web = site.OpenWeb(WebUrls.Blog))
                     {
                        // query.Webs = "<Webs Scope='SiteCollection' />";
                         query.Webs = "<Webs Scope=\"Recursive\">";
                         query.Lists = "<Lists ServerTemplate=\"105\" />";
                         query.ViewFields = "<FieldRef Name=\"Title\" />";
                      
                         DataTable dt = web.GetSiteData(query);

    Wednesday, August 21, 2013 2:51 PM

Answers

  • Hi,

    Based on your description, I understand that you want to get all sub sites and all the lists in the sub sites.

    The code bellow for your reference.

    1.Get all sub sites and their URLs and Titles.

    using (SPSite spSite = new SPSite("http://sharepoint2013/"))
    {
        foreach (SPWeb spWeb in spSite.AllWebs)
        {
            System.Console.WriteLine("SiteURL:{0}------SiteTitle:{1}",spWeb.Url,spWeb.Title);
        }   
    }

    2.Get all the lists in the sub sites.

    using (SPSite spSite = new SPSite("http://sharepoint2013/"))
    {
        foreach (SPWeb spWeb in spSite.AllWebs)
        {
            foreach(SPList spList in spWeb.Lists)
            {
                System.Console.WriteLine("ListTile:{0}", spList.Title);
            }       
         }   
    }

    More information:

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

    Best Regards,

    Dennis Guo



    • Edited by Dennis Guo Thursday, August 22, 2013 8:56 AM
    • Marked as answer by Dennis Guo Thursday, August 29, 2013 2:28 PM
    Thursday, August 22, 2013 8:52 AM

All replies

  • Hi,

    Based on your description, I understand that you want to get all sub sites and all the lists in the sub sites.

    The code bellow for your reference.

    1.Get all sub sites and their URLs and Titles.

    using (SPSite spSite = new SPSite("http://sharepoint2013/"))
    {
        foreach (SPWeb spWeb in spSite.AllWebs)
        {
            System.Console.WriteLine("SiteURL:{0}------SiteTitle:{1}",spWeb.Url,spWeb.Title);
        }   
    }

    2.Get all the lists in the sub sites.

    using (SPSite spSite = new SPSite("http://sharepoint2013/"))
    {
        foreach (SPWeb spWeb in spSite.AllWebs)
        {
            foreach(SPList spList in spWeb.Lists)
            {
                System.Console.WriteLine("ListTile:{0}", spList.Title);
            }       
         }   
    }

    More information:

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

    Best Regards,

    Dennis Guo



    • Edited by Dennis Guo Thursday, August 22, 2013 8:56 AM
    • Marked as answer by Dennis Guo Thursday, August 29, 2013 2:28 PM
    Thursday, August 22, 2013 8:52 AM
  • thank you
    • Marked as answer by Dennis Guo Thursday, August 29, 2013 2:28 PM
    • Unmarked as answer by Dennis Guo Thursday, August 29, 2013 2:28 PM
    Monday, August 26, 2013 6:09 AM