1

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...

1 Answer 1

2

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();
    }       
4
  • yes this way gud... but i looking for direct property or methods available... So we reduce and iterate loops for getting subwebs.. Jan 21, 2013 at 7:32
  • as par my knowledge : only one direct method available like SPWebCollection webcoll= SPWeb.Webs for this requirement Jan 21, 2013 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 Jan 21, 2013 at 8:39
  • ohh yes!! i forget to mention it.i have updated my answer. thanks Jan 21, 2013 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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