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

I've been trying to use the method shown here:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistcollection.aspx

However, I'm getting this error on line 2 below:

Microsoft.SharePoint.SPWebCollection: type used in a using statement must be implicitly convertible to 'System.IDisposable'

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebs = oSiteCollection.AllWebs)
{
    foreach (SPWeb oWebsite in collWebs)
    {
        SPListCollection collSiteLists = oWebsite.Lists;
        foreach (SPList oList in collSiteLists)
        {
            Label1.Text += SPEncode.HtmlEncode(oWebsite.Name) + " :: " +
                SPEncode.HtmlEncode(oList.Title) + "<BR>";
        }
        oWebsite.Dispose();
    }
}

It won't allow SPWebCollection to be used in a using statement, because it doesn't inherit IDisposable.

Did MSDN make an error, or am I missing something?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can surely expect errors in MSDN documentation. Right below the page, there is a Community Content area in which you can suggest them to correct the error or warn the readers of the MSDN content.

It can be corrected like:

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebs = oSiteCollection.AllWebs

    foreach (SPWeb oWebsite in collWebs)
    {
        using(oWebsite)
        {
            SPListCollection collSiteLists = oWebsite.Lists;
            foreach (SPList oList in collSiteLists)
            {
                Label1.Text += SPEncode.HtmlEncode(oWebsite.Name) + " :: " +
                    SPEncode.HtmlEncode(oList.Title) + "<BR>";
            }
        }
    }
share|improve this answer
    
Excellent, fast response. Thanks Ashish. –  Wesley Oct 4 '12 at 18:49

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.