Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

SPListCollection class

Represents a collection of SPList objects.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
[SubsetCallableTypeAttribute]
[ClientCallableTypeAttribute(Name = "ListCollection", CollectionChildItemType = typeof(SPList), 
	CollectionIndexerMethodClientName = "GetById", CollectionCreateEntityMethodClientName = "Create", 
	ServerTypeId = "{1E78B736-61F0-441c-A785-10FC25387C8D}", SampleUrl = "{apiroot}/web/lists")]
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 Microsoft SharePoint Foundation 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>";
        }
        oWebsite.Dispose();
    }
}
NoteNote

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 Disposing 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.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.