Partager via


Returning Items from a List

To return items from a list, you can instantiate an SPWeb object and drill down through the object model to the SPListItemCollection object for the list. Once you return the collection of all items for a list, you can iterate through the collection and use indexers to return specific field values.

The following example returns all the items for a specified events list. It assumes the existence of a text box for typing the name of an events list.

[Visual Basic .NET]

Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
Dim listItems As SPListItemCollection = mySite.Lists(TextBox1.Text).Items
Dim i As Integer

For i = 0 To listItems.Count - 1

    Dim item As SPListItem = listItems(i)

    Response.Write(SPEncode.HtmlEncode(item("Title").ToString()) & " :: " _
        & SPEncode.HtmlEncode(item("Location").ToString()) & " :: " _
        & SPEncode.HtmlEncode(item("Begin").ToString()) & " :: " _
        & SPEncode.HtmlEncode(item("End").ToString()) & "<BR>")

Next i

[C#]

SPWeb mySite = SPControl.GetContextWeb(Context);
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;

for (int i=0;i<listItems.Count;i++)
{
    SPListItem item = listItems[i];

    Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + " : " +
        SPEncode.HtmlEncode(item["Location"].ToString()) + " : " +
        SPEncode.HtmlEncode(item["Begin"].ToString()) + " : " +
        SPEncode.HtmlEncode(item["End"].ToString()) + "<BR>");
}

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint, Microsoft.SharePoint.Utilities, and Microsoft.SharePoint.WebControls namespaces.

In the example, indexers are used both to return the list typed by the user and to return specific items from the list. To return the items, the indexers specify the name of each column whose value is returned. In this case all the field names pertain to a common Events list.

You can also use one of the GetItems methods of the SPList class to return a subset of items from a list. The following example returns only Title column values where the Stock column value surpasses 100.

[Visual Basic .NET]

Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
Dim list As SPList = mySite.Lists("Books")

Dim query As New SPQuery()
query.Query = "<Where><Gt><FieldRef Name='Stock'/><Value Type='Number'>100</Value></Gt></Where>"

Dim myItems As SPListItemCollection = list.GetItems(query)
Dim item As SPListItem

For Each item In  myItems

    Response.Write(SPEncode.HtmlEncode(item("Title").ToString()) & "<BR>")

Next item

[C#]

SPWeb mySite = SPControl.GetContextWeb(Context);
SPList list = mySite.Lists["Books"];

SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name='Stock'/><Value Type='Number'>100</Value></Gt></Where>";

SPListItemCollection myItems = list.GetItems(query);

foreach (SPListItem item in myItems)
{
    Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}

The previous example uses a constructor to instantiate an SPQuery object, and then assigns to the Query property of the query object a string in Collaborative Application Markup Language (CAML) that specifies the inner XML for the query (in other words, the Where element). Once the Query property is set, the query object is passed through the GetItems method to return and display items.

The previous example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint, Microsoft.SharePoint.Utilities, and Microsoft.SharePoint.WebControls namespaces.

The example assumes the existence of a Books list that has a Stock column containing number values.