Tag Archives: Lists and Libraries in the Object Model

Lists and Libraries in the Object Model – Microsoft SharePoint 2010

The functionality that features provide to define and create lists is useful, but the static nature of XML means that you need more tools at your disposal to fully exploit the power of lists and libraries. Fortunately, just as you’d expect, the core SharePoint assembly contains a robust set of classes to allow automation of lists and their contents.

As you explore these classes, you may find yourself in familiar territory: The names may be new, but the techniques closely resemble those that you use to manipulate any .NET collection. The techniques for adding, editing, and deleting list items resemble those for every data access stack from Microsoft since Windows development first became popular in the 1990s.

In fact, the only unusual aspect of working with SharePoint lists and libraries is the query syntax which you will see in a later section.

You can accomplish a great deal in code with a relatively small number of
classes:

  • SPListCollection
  • SPList
  • SPDocumentLibrary
  • SPListItemCollection
  • SPListItem
  • SPListView
  • SPContentType
  • SPField

Each SPWeb instance exposes an SPListCollection via the Lists property that contains all of the SPList instances in the web. Some of the SPList instances are document libraries and are actually of type SPDocumentLibrary. Each SPList and SPDocumentLibrary exposes its items via the Items property, which is of type SPListItemCollection. Each item is an SPListItem.

The other classes, SPListView, SPContentType, and SPField represent a list’s presentation and structure.

Retrieving List Instances

If you need to access a list within a page context, perhaps as part of a custom list form, you can get the current List or ListId from SPContext.Current. In other situations, feature event receivers for instance, you access a list instance via the containing web.

Lists and libraries are members of a specific web. Therefore, instances of the SPWeb class expose a property named Lists that returns an instance of SPListCollection that you can use to enumerate all of the lists in the web.

The SPListCollection Item property has three overloads that you can use to retrieve a specific list instance. One takes the list’s GUID, one takes the list’s name, and the other takes the list’s ordinal position in the collection.

Alternatively, you can use the SPWeb.GetList method to retrieve a list based on its URL. This method is especially versatile because the URL can point directly to the list or to any of its forms using an absolute or site relative format.

doug (frame 367 of smile clip)This post is an excerpt from the online courseware for our Microsoft SharePoint 2010 for Developers course written by expert Doug Ware.

Doug Ware is a SharePoint expert and an instructor for many of our SharePoint 2007 and SharePoint 2010 courses. A Microsoft MVP several times over, Doug is the leader of the Atlanta .NET User Group, one of the largest user groups in the Southeast U.S., and is a frequent speaker at code camps and other events. In addition to teaching and writing about SharePoint, Doug stays active as a consultant and has helped numerous organizations implement and customize SharePoint.