SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to get All Subwebs upto N Levels for Specified Web.


My Site url is...


I need all subwebs upto N Levels for web1..

below line get all subwebs upto N Levels of sitecollection

SPWebCollection allwebcoll = spsite.AllWebs;

but i dont need all subwebs upto N Levels of site collection.

I need all subwebs upto N Levels for specified web.


share|improve this question
up vote 2 down vote accepted

if you want to recursive for specific SPWeb than

try this :

   SPWeb web = SPContext.Current.Web;

function for recursive :

 protected void recursive(SPWeb web)
        string webTitle = web.Title;

        if (web.Webs.Count > 0)
            foreach (SPWeb oweb in web.Webs)

       if(web != null)
share|improve this answer
yes this way gud... but i looking for direct property or methods available... So we reduce and iterate loops for getting subwebs.. – Hitesh Chandegara Jan 21 '13 at 7:32
as par my knowledge : only one direct method available like SPWebCollection webcoll= SPWeb.Webs for this requirement – Jigs Jan 21 '13 at 7:38
remember to dispose! Several examples of what you want to achieve on this site like here and yes you need to recursively call .Webs and set a "depth" counter for each recursion until N has been reached – Anders Rask Jan 21 '13 at 8:39
ohh yes!! i forget to mention it.i have updated my answer. thanks – Jigs Jan 21 '13 at 8:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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