SPList class (Microsoft.SharePoint) (original) (raw)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
SPList class
- Article
- 02/13/2018
In this article
Represents a list on a Microsoft SharePoint Foundation Web site.
Inheritance hierarchy
System.Object
Microsoft.SharePoint.SPSecurableObject
Microsoft.SharePoint.SPList
Microsoft.SharePoint.SPDocumentLibrary
Microsoft.SharePoint.SPIssueList
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaration
Public Class SPList _
Inherits SPSecurableObject
'Usage
Dim instance As SPList
public class SPList : SPSecurableObject
Remarks
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.
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: " + 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
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: " +
SPHttpUtility.HtmlEncode(oListItem["Title"].ToString()) +
"::" + "Value: " +
SPHttpUtility.HtmlEncode(oListItem["Investment"].ToString()) +
"::" + "Calculated: " +
SPHttpUtility.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 (CAML) schemas.
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
Microsoft.SharePoint namespace