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

See below:

SPWeb currentWeb = SPContext.Current.Web;
foreach (var web in currentWeb.Webs)
{
    // web is object here - why?
}

SPWebCollection implements IEnumerable<SPWeb> (among other things), so why doesn't it work?

The project framework is .NET 3.5, and I have other .NET 3.5 stuff in the same code.

Or is this just me? Can anyone reproduce?

share|improve this question
1  
Are you building it in .NET 2 or greater? –  Sébastien Levert Feb 17 '11 at 17:37
    
Make sure in this model you dispose of things properly too. (use the try-finally method). Read this for more info: sharepointnutsandbolts.com/2008/06/… –  James Love Feb 17 '11 at 18:24
    
@Sébastien Yes, project framework is .NET 3.5. I have other .NET 3.5 stuff in my code. –  ScarePoint Feb 17 '11 at 19:19
    
@James Thanks. :) –  ScarePoint Feb 17 '11 at 19:20
add comment

1 Answer

You haven't specified what your actual problem is, unless you are wondering why the variable "web" is an object. The reason it is an object is because you defined it as a "var", not an SPWeb. If you built your foreach loop like this:

SPWeb currentWeb = SPContext.Current.Web;
foreach (SPWeb web in currentWeb.Webs)
{
// web is now an SPWeb object.
}
share|improve this answer
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.