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

If I have to grab numerous lists which I have the name, URL or Guid of, should I use web.Lists and return the entire SPListCollection or grab the specific ones I need via web.Lists[Guid] and place them in List?

Other then using the SPListCollection.Add method, what are the benefits using SPListCollection vs a collection of SPList?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

This answer has two parts:

First, for maximum performance and scalability, to retrieve the lists you should be using web.Lists[Guid]. Iterating through web.Lists and picking your selected lists, or using web.Lists[Name] (which also iterates through the set internally) will causes the API to retrieve metadata for all SPList objects under that SPWeb, which can potentially be a huge.

That means you would be using a List to collect them, since you cannot create a SPListCollection except from web.Lists or similar calls (SPListCollection has no public constructor, so you cannot do 'new SPListCollection' and build your own).

Second, if you get down to the basics you should really be careful building a List< SPList >. In many cases, manipulating a SPList object on its own will cause the API to access its parent SPWeb (for example, by using SPList.GetItems(SPQuery)). If you have already disposed of it, you might incur perfomance hits. You should keep your manipulations of SPList close to the scope of its parent SPWeb object, and implement proper (not related to SP* objects) interfaces and/or repositories if you want to expose SPList or SPWeb data outside their scope. I would avoid List< SPList > entirely in most cases.

Side note on the difference between SPListCollection and List< SPList >:

SPListCollection is, as are most collections in SharePoint's API, based on SPBaseCollection. That class is implementing ICollection (non-generic) instead of the new, much more feature-full and extensible generic ICollection(Of T). You could transform a SPListCollection into a List< SPList> (or better, ICollection< SPList>), but that would violate the best practice given above.

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.