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)

'Declaration
Public Function GetItems ( _
	query As SPQuery _
) As SPListItemCollection
'Usage
Dim instance As SPList
Dim query As SPQuery
Dim returnValue As SPListItemCollection

returnValue = instance.GetItems(query)

Parameters

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.

Dim siteCollection As SPSite = SPContext.Current.Site
Try
    Dim list As SPList = siteCollection.AllWebs("Site_Name").Lists("List_Name")
    Dim query As New SPQuery()
    query.Query = "<Where><Eq><FieldRef Name='Schedule'/>" + 
        "<Value Type='CHOICE'>2 weeks</Value></Eq></Where>"
    Dim listItems As SPListItemCollection = list.GetItems(query)

    Dim listItem As SPListItem
    For Each listItem In  listItems
        Label1.Text += SPEncode.HtmlEncode(listItem("Title").ToString()) + 
        " -- " + SPEncode.HtmlEncode(listItem("EndDate").ToString()) + "<BR>"
    Next listItem
Finally
    siteCollection.Dispose()
End Try

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.

Show: