This documentation is archived and is not being maintained.

SPList.GetItems Method (SPQuery)

Windows SharePoint Services 3

Returns a collection of items from the list based on the specified query.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPListItemCollection GetItems(
	SPQuery query


Type: Microsoft.SharePoint.SPQuery
An SPQuery object that contains the query.

Return Value

Type: Microsoft.SharePoint.SPListItemCollection
An SPListItemCollection object that represents the items.

The following code example uses the GetItems method to return and display items with a "Schedule" field value that equals "2 weeks".

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

The example assumes the existence of an .aspx page that contains a label control.

SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='Schedule'/>" +
        "<Value Type='CHOICE'>2 weeks</Value></Eq></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

foreach (SPListItem oListItem in collListItems)
    Label1.Text += SPEncode.HtmlEncode(oListItem["Title"].ToString()) 
        + " -- " + SPEncode.HtmlEncode(oListItem["EndDate"].ToString())   
        + "<BR>";

After instantiating an SPQuery object through a constructor, the example uses Collaborative Application Markup Language Core Schemas to define criteria for the query, which is passed as parameter in the GetItems method.