Scenario 2: How to iterate through items in a SharePoint list using SPList?
Lets start by looking at a code snippet that can be used in a WebPart to access the first 100 items from the SharePoint list of the current context:
SPList activeList = SPContext.Current.List; for(int i=0;i<100 && i<activeList.Items.Count;i++) { SPListItem listItem = activeList.Items[i]; htmlWriter.Write(listItem["Title"]); }
Assuming that there are at least 100 items in the list. How many roundtrips to the database is this code going to make in order to retrieve the 100 Title’s of the first 100 SharePoint list items? You might be surprised. Its a total of 200 database calls as you can see from the database view when analyzing the transaction executing the above code:
The reason for that is because in every loop we request a new SPListItemCollection object when accessing the Items property. The Items property is not cached and therefore always requests all items from the database again. Here is how the first loop iterations look like in the PurePath:
The CORRECT way to do it
The correct way to do it is of course to store the Items property return value in a SPListItemCollection variable. With this the database is only queried once and we will then iterate over the result set that is stored within the collection object. Here is the changed sample code:
SPListItemCollection items = SPContext.Current.List.Items; for(int i=0;i<100 && i<items.Count;i++) { SPListItem listItem = items[i]; htmlWriter.Write(listItem["Title"]); }
Resulting in the following PurePath.
Conclusion
Many properties in SharePoint return new object instances every time you access them. In order to build good software based on the Microsoft SharePoint Platform its necessary to understand what is going on under the hood. This will eliminate “surprises” once your custom code is first executed with real life data.
There are additional ways to optimize access to data stored in SharePoint lists. I will cover that in my next posts.
Related posts:
- SharePoint: Only request data that you really need One of the main performance problems that we can witness...
- Performance Considerations when using SharePoint Object Model The SharePoint Object Model allows external applications or hosted WebParts...
- SharePoint: Page through SharePoint lists SharePoint lists can contain thousands of items. We have all heard...
- SharePoint: Using Batch-Updates to speed up performance? In my previous posts about SharePoint I focused on how...
- SharePoint: Monitoring individual List usage and performance We all know that SharePoint list performance can degrade the...


Very nice article and defenitely a permanent link to your blog from my blog
Keep up the good work!
[...] The right and wrong ways to iterate through SharePoint List items: http://blog.dynatrace.com/2009/01/11/the-wrong-way-to-iterate-through-sharepoint-splist-items/ [...]
I wonder if you tested this behavior in Release build mode?
I think that it could work normally, because VS will optimize your calls
The above examples behave the same way in debug or release. Accessing the property is actually invoking the get accessor method. As the get accessor is in fact a method the compiler cannot just optimize the call for you as there can be logic in the accessor that needs to be exected each time the property is accessed.
There is however a better way to iterate through the list – namely – using a foreach loop. Maybe thats what you are refering to.
The foreach loop is compiled in a way that the propertie’s IEnumerator return value is stored in a local variable. This IEnumerator is then used to iteratore over each element.
Gute Arbeit hier! Gute Inhalte.
[...] how to iterate, check this blog. Possibly related posts: (automatically generated)Java generics gotchaMore ArcGIS Geoprocessing [...]