This documentation is archived and is not being maintained.

SPListCollection Class

Windows SharePoint Services 3

Represents a collection of SPList objects.

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

[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPListCollection : SPBaseCollection

Use the Lists property of the SPWeb class to return an SPListCollection object that represents the collection of lists in a site, or use the Lists property of the SPList class to return an SPListCollection object that represents the collection of parent lists for a list. To create a list, use one of the Add methods of SPListCollection.

Use an indexer to return a single list from the collection. For example, assuming 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.

If users who are running the code have full permission for a list but do not have permissions to the site, use the GUID indexer to return a list from the collection of lists in the site. Otherwise, an Access Denied error message is returned because the user is not allowed to enumerate the collection of lists, as is done when the name indexer is used. The GUID indexer allows direct access to the item because the GUID is the primary key in the Lists table of the Windows SharePoint Services database.

The following code example iterates through all the lists of all the sites in a site collection and displays the name of each list and site. 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.

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWebCollection collWebs = oSiteCollection.AllWebs)
    foreach (SPWeb oWebsite in collWebs)
        SPListCollection collSiteLists = oWebsite.Lists;
        foreach (SPList oList in collSiteLists)
            Label1.Text += SPEncode.HtmlEncode(oWebsite.Name) + " :: " +
                SPEncode.HtmlEncode(oList.Title) + "<BR>";

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

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