Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

eg.

My Site url is...

"http://server/web1"

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.

Thanks...

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

if you want to recursive for specific SPWeb than

try this :

   SPWeb web = SPContext.Current.Web;
   recursive(web);

function for recursive :

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

        if (web.Webs.Count > 0)
        {
            foreach (SPWeb oweb in web.Webs)
            {
                recursive(oweb);
            }
        }

       if(web != null)
          web.Dispose();
    }       
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
add comment

Your Answer

 
discard

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.