Click to Rate and Give Feedback
SPList Class (Microsoft.SharePoint)
Represents a list on a SharePoint Web site.

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

Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPList
    Implements ISecurableObject
Visual Basic (Usage)
Dim instance As SPList
C#
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPList : ISecurableObject

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.

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.

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.

Visual Basic
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
C#
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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Inadvertent Italic Tags?      M. Blumenthal ... Thomas Lee   |   Edit   |  

In the code samples above, I think there are HTML italic tags that are being displayed as HTML instead of interpreted.

Currently, the code reads:

SPList list = siteCollection.<i>AllWebs</i>["<i>Site_Name</i>"].Lists["<i>List_Name</i>"];

However, I am pretty sure it should be more like this:

SPList list = siteCollection.AllWebs[YourSiteNameHere].Lists[YourListNameHere];
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker