locked
How to get immediate sub-site for Sharepoint web site? RRS feed

  • Question

  • Hi All...

    I have a web application as breakfast.com and site collection inside it as fruit
    http://www.breakfast.com/fruit/default.aspx

    Now, there are 2 sub-sites under fruit site collection

    http://www.breakfast.com/fruit/apple/default.aspx
    http://www.breakfast.com/fruit/mango/default.aspx

    And more sub-sites under each of them

    http://www.breakfast.com/fruit/apple/aaa/default.aspx
    http://www.breakfast.com/fruit/apple/bbb/default.aspx
    http://www.breakfast.com/fruit/apple/ccc/default.aspx

    http://www.breakfast.com/fruit/mango/xxx/default.aspx
    http://www.breakfast.com/fruit/mango/yyy/default.aspx
    http://www.breakfast.com/fruit/mango/zzz/default.aspx

    Now the issue....

    If I use SPSite, then I can retrieve all URLs using AllWebs property.

    I would get following URLs for http://www.breakfast.com/fruit/default.aspx

    http://www.breakfast.com/fruit/apple/default.aspx
    http://www.breakfast.com/fruit/apple/aaa/default.aspx
    http://www.breakfast.com/fruit/apple/bbb/default.aspx
    http://www.breakfast.com/fruit/apple/ccc/default.aspxhttp://www.breakfast.com/fruit/mango/default.aspx
    http://www.breakfast.com/fruit/mango/xxx/default.aspx
    http://www.breakfast.com/fruit/mango/yyy/default.as
    http://www.breakfast.com/fruit/mango/zzz/default.as

    But my requirement is, from any sub-site, I need only its immediate sub-sites

    E.g. For http://www.breakfast.com/fruit/apple/default.aspx, I should get only

    http://www.breakfast.com/fruit/apple/aaa/default.aspx
    http://www.breakfast.com/fruit/apple/bbb/default.aspx
    http://www.breakfast.com/fruit/apple/ccc/default.aspx

    Thanx in advance....'
    Wednesday, September 17, 2014 4:07 AM

Answers

  • You will need to open the web and then use Webs property, it will return only 1st level sub sites.

     using (SPSite site = new SPSite("..") )
                {
                   var webCollection = site.RootWeb.Webs;
                }
    

    Similarly if you want sub sites from any other web then open that web and use 'Webs' property.


    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Proposed as answer by Hemendra AgrawalModerator Wednesday, September 17, 2014 11:00 AM
    • Marked as answer by Suyog ' Thursday, September 18, 2014 6:39 AM
    • Unmarked as answer by Suyog ' Thursday, September 18, 2014 7:04 AM
    • Marked as answer by Suyog ' Monday, September 29, 2014 2:13 AM
    Wednesday, September 17, 2014 6:41 AM

All replies

  • You will need to open the web and then use Webs property, it will return only 1st level sub sites.

     using (SPSite site = new SPSite("..") )
                {
                   var webCollection = site.RootWeb.Webs;
                }
    

    Similarly if you want sub sites from any other web then open that web and use 'Webs' property.


    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Proposed as answer by Hemendra AgrawalModerator Wednesday, September 17, 2014 11:00 AM
    • Marked as answer by Suyog ' Thursday, September 18, 2014 6:39 AM
    • Unmarked as answer by Suyog ' Thursday, September 18, 2014 7:04 AM
    • Marked as answer by Suyog ' Monday, September 29, 2014 2:13 AM
    Wednesday, September 17, 2014 6:41 AM
  • Thanx a lot . . '
    It worked . . .

    -------------------------------------------

    But, SPSite object holds only site collection.....
    Even if we pass any sub-site in it, it is converted to site collection.

    Is there any way where I could get output from any sub-site(not only Site Collection)?

    • Edited by Suyog ' Thursday, September 18, 2014 7:08 AM Updating Issue
    Thursday, September 18, 2014 6:43 AM