3

I have a root site with sites at different levels(depth is unknown). How can I find all the SPWeb objects iterating till the deepest level.

will the below code give me desired result

SPWebApplication mySPWebApp = SPWebApplication.Lookup(new Uri("http://test-site"));
{
    foreach (SPSite siteCollection in mySPWebApp.Sites)
    {
        foreach (SPWeb oweb in siteCollection.AllWebs)
        {
            //My Code
        }
    }
}

2 Answers 2

7

Yes, with SPWebApplication.Sites you get all site collections in a web application and with SPSite.AllWebs you get all sites in a site collection no matter the level.

Best practice is to dispose explicitly of individual Web sites that are retrieved from the collection that is returned through the AllWebs property.

foreach (SPWeb oweb in siteCollection.AllWebs)
{
     try
     {
           //Your code
     }
     finally
     {
          oweb.Dispose();
     }
}

You should dispose site collections too.

3
  • 1
    Disposing is good. Best practice is to dispose in a try catch or using statement
    – Tom Resing
    Jun 3, 2014 at 21:07
  • 1
    @TomResing The “best practice” sentence in my answer is from Microsoft, that’s why I’ve put that as a quote. I agree that “best practice” is overused and sometimes misused (not just by Microsoft) and very often it should be “good to”, “should”, or “must” instead of “best practice”. I’ll edit my answer to add your suggestion for try/finally. Thanks. Jun 4, 2014 at 7:45
  • Good update @naim!
    – Tom Resing
    Jun 4, 2014 at 16:28
3

To build on @naim's answer, you should dispose both the Site Collections and the webs and use try/catch around the dispose or using statements.

Site Collection Dispose Example

from Best Practices: Using Disposable Windows SharePoint Services Objects

void SPSiteCollectionForEachNoLeak()
{
    using (SPSite siteCollectionOuter = new SPSite("http://moss"))
    {
        SPWebApplication webApp = siteCollectionOuter.WebApplication;
        SPSiteCollection siteCollections = webApp.Sites;

        foreach (SPSite siteCollectionInner in siteCollections)
        {
            try
            {
                // ...
            }
            finally
            {
                if(siteCollectionInner != null)
                    siteCollectionInner.Dispose();
            }
        }
    } // SPSite object siteCollectionOuter.Dispose() automatically called.
}

Web Dispose Example

from Best Practices: Using Disposable Windows SharePoint Services Objects

void SPWebCollectionAddNoLeak(string strWebUrl)
{
    using (SPSite siteCollection = new SPSite("http://moss"))
    {
        using (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            SPWebCollection webCollection = siteCollection.AllWebs; // No AllWebs leak just getting reference.
            using (SPWeb innerWeb = webCollection.Add(strWebUrl))
            {
                //...
            }
        } // SPWeb object outerWeb.Dispose() automatically called.
    }  // SPSite object siteCollection.Dispose() automatically called. 
}

Your Answer

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

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