Share via


SPListCollection Class

Represents a collection of SPList objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPListCollection

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

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPListCollection _
    Inherits SPBaseCollection

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

Remarks

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.

Examples

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.

Dim site As SPSite = SPControl.GetContextSite(Context)
Dim allSites As SPWebCollection = site.AllWebs
Dim subSite As SPWeb

For Each subSite In  allSites

    Dim allSiteLists As SPListCollection = subSite.Lists
    Dim subSiteList As SPList

    For Each subSiteList In  allSiteLists

        Label1.Text += SPEncode.HtmlEncode(subSite.Name) & " :: " & _
           SPEncode.HtmlEncode(subSiteList.Title) & "<BR>"

    Next subSiteList

Next subSite 
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>";
        }
        oWebsite.Dispose();
    }
}

Note

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.

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

SPListCollection Members

Microsoft.SharePoint Namespace