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.



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)
|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 – Jignesh Rajput 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 – Jignesh Rajput Jan 21 '13 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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