Export (0) Print
Expand All

SPList class

Represents a list on a Microsoft SharePoint Foundation Web site.

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

'Declaration
Public Class SPList _
	Inherits SPSecurableObject
'Usage
Dim instance As SPList

A list consists of items or rows, and columns or fields, that contain data. The Items property returns the collection of items in the list, and the Fields property returns the collection of fields in the list. To improve performance, it is best practice to use one of the GetItem* methods to return a filtered collection of items from the list.

Various SPList properties, ParentList properties, and other properties or methods for classes in the Microsoft.SharePoint namespace return a list or collection of lists from a specific context. Otherwise, use the Lists property of either the SPWeb or SPList class to return an SPListCollection object that represents either the collection of lists in a site or the collection of parent lists for a list. Use an indexer to return a single list from the collection. For example, if the collection is assigned to a variable named collLists, use collLists[index] in C#, or collLists(index) in Visual Basic, where index is the index number of the list in the collection, the display name of the list, or the GUID of the list.

The following code example returns and displays items from a specified list where values in the "ProjectedValue" field are greater than 500. The example assumes the existence of an .aspx page that contains a label control.

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

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><Gt><FieldRef Name='ProjectedValue'/>" + "<Value Type='Number'>500</Value></Gt></Where>"
    Dim listItems As SPListItemCollection = list.GetItems(query)
            
        Dim listItem As SPListItem
        For Each listItem In  listItems
            Label1.Text += "Item: " + SPHttpUtility.HtmlEncode(listItem("Title").ToString()) + 
            "::" + "Value: " + SPHttpUtility.HtmlEncode(listItem("Investment").ToString()) + 
            "::" + "Calculated: " + SPHttpUtility.HtmlEncode(listItem("ProjectedValue").ToString()) + "<BR>"
        Next listItem
Finally
    siteCollection.Dispose()
End Try

After instantiating an SPQuery object, the example uses Collaborative Application Markup Language (CAML) to define criteria for the query, which is passed as a parameter in the GetItems method. For information about CAML, see Collaborative Application Markup Language (CAML) schemas.

NoteNote

For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft