Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPList Class
SPList Properties
 Items Property

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
SPList.Items Property (Microsoft.SharePoint)
Gets the collection of all items in the list. This property returns all metadata of each item in the list and can greatly diminish performance. It is recommended that you instead use the GetItems method to return items from a list, which allows you to specify which data to return.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Overridable ReadOnly Property Items As SPListItemCollection
Visual Basic (Usage)
Dim instance As SPList
Dim value As SPListItemCollection

value = instance.Items
C#
public virtual SPListItemCollection Items { get; }

Property Value

An SPListItemCollection object that represents the collection of items.

The Items property returns all the files in a document library, including files in subfolders, but not the folders themselves. In a document library, folders are not considered items.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Clarification / Bug / Bad Documentation      anonymous coward   |   Edit   |   Show History
The Items property doesn't seem to get *all* items. I have a folder with ID=1 on a document library and mysplist.items.getItemById(1) throws an exception whereas mysplistitem.getItemById(1) returns my folder.
Beware of overusing SPList.Items      austegard   |   Edit   |   Show History

There's far too much of this type of code floating around*:

SPList list = web.Lists["Web Part Gallery"];
// go through the items in reverse
for (int i = list.ItemCount - 1; i >= 0; i--)
{
    string name = list.Items[i].Name;
    if (name == someCondition)
    {
        list.Items[i].SomeAction();
    }
}

*copied from here: http://cks.codeplex.com/SourceControl/changeset/view/26423#358362
** this particular code also incorrectly disposes the Feature's site and web, but that's another story

Looks innocuous enough, but look at the internals of the SPList.Items property:

public virtual SPListItemCollection Items
{
get
{
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
return this.GetItems(query);
}
}

This means the code above runs a recursive query accross all list items every time the indexer is accessed, so in the code above if there are, say 10 webparts, the webpart list is queried 20 times.

The better way to do this is to create a local SPListItemCollection instance and use it in the loop, the query is now run only once:

SPListItemCollection webParts = web.Lists["Web Part Gallery"].Items;
// go through the items in reverse
for (int i = webParts.Count - 1; i >= 0; i--)
{
string webpartName = webParts[i].Name;
if (webpartName == someCondition)
{
webParts[i].SomeAction();
}
}


Also see http://mo.notono.us/2009/03/beware-of-splistitemsadd.html for a warning regarding SPList.Items.Add()

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker