Share via


SPList Class

Represents a list on a SharePoint Web site.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPList
    Microsoft.SharePoint.SPDocumentLibrary
    Microsoft.SharePoint.SPIssueList

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

Syntax

<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPList _
    Implements ISecurableObject

Dim instance As SPList
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPList : ISecurableObject

Remarks

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

Various List 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 2005, where index is the index number of the list in the collection, the display name of the list, or the GUID of the list.

Examples

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: " + SPEncode.HtmlEncode(listItem("Title").ToString()) + 
            "::" + "Value: " + SPEncode.HtmlEncode(listItem("Investment").ToString()) + 
            "::" + "Calculated: " + SPEncode.HtmlEncode(listItem("ProjectedValue").ToString()) + "<BR>"
        Next listItem
Finally
    siteCollection.Dispose()
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];

SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Gt><FieldRef Name='ProjectedValue'/>" +
    "<Value Type='Number'>500</Value></Gt></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

foreach (SPListItem oListItem in collListItems)
{
    Label1.Text += "Item: " + 
        SPEncode.HtmlEncode(oListItem["Title"].ToString()) + 
        "::" + "Value: " +   
        SPEncode.HtmlEncode(oListItem["Investment"].ToString()) +
        "::" + "Calculated: " + 
        SPEncode.HtmlEncode(oListItem["ProjectedValue"].ToString()) + 
        "<BR>";
    }
}

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 Core Schemas.

In addition, the example uses indexers on each list item to return values from the specified fields.

Thread Safety

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

See Also

Reference

SPList Members

Microsoft.SharePoint Namespace