Export (0) Print
Expand All
Expand Minimize

SPList.Items Property

Windows SharePoint Services 3

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)

public virtual SPListItemCollection Items { get; }

Property Value

Type: Microsoft.SharePoint.SPListItemCollection
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.

When you call the Items property, it returns an instance of an SPListItemCollection object that does not contain any data, but on first access to an item from the collection, the entire collection object is filled with data. Consequently, to improve performance it is recommended that you assign the items returned by Items to an SPListItemCollection object if you must iterate the entire collection, as seen in the example. It is best practice is to use one of the GetItem* methods of SPList to return a filtered collection of items.

The following example displays the items of a Project list within a label. The example assigns the items that are returned by the Items property to an SPListItemCollection object and then iterates through the collection. The example does not use oList.Items[i] within the for loop.

using (SPWeb oWebsite = new SPSite("http://Server/sites/SiteCollection").OpenWeb())
{

    SPList oList = oWebsite.Lists["Projects"];

    SPListItemCollection collItem = oList.Items;

    for (int i = 0; i < oList.ItemCount; i++)
    {
        string itemName = collItem[i].Name;

        Label1.Text += itemName + "<BR>";

    }
}

Community Additions

ADD
Show:
© 2014 Microsoft