Accessing SharePoint List Items

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This QuickStart demonstrates the following three patterns for programmatically accessing SharePoint list items:

  • The Direct SharePoint list access pattern. This pattern uses the SharePoint object model directly, with no intervening layers of abstraction.
  • The List Item Repository access pattern. This pattern abstracts calls to SharePoint lists. It uses a programmer-provided repository class.
  • The Entity Repository access pattern. This pattern provides two abstraction layers. It uses business entities with entity repositories and a ListItemRepository class.

For more information about the repository pattern, see Design Patterns and The Application Structure.

The SPGSharePointDataAccessQuickStart.sln file defines the QuickStart. The Visual Studio solution contains five projects. The main project is SPGSharePointDataAccessQuickStart.SharePoint. This project deploys a custom SharePoint application page that demonstrates how to access SharePoint list items. This page is deployed to _layouts/SPG/SPGSharePointDataAccessQuickStart.aspx on the SharePoint Web application. (The QuickStart is configured to interact with a SharePoint Announcements list that has the title Announcements. The list is in the root site in the site collection that the QuickStart is deployed to.)

QuickStart Setup

This section describes how to set up the QuickStart. The site collection that the QuickStart is deployed to requires an Announcements list at the root site.

To set up the QuickStart

  1. In Visual Studio, open SPGSharePointDataAccessQuickStart.sln.
  2. Right-click the SPGSharePointDataAccessQuickStart.SharePoint project, and then click Properties.
  3. On the Debug tab, enter the URL of your local SharePoint test instance. The default is https://localhost.
  4. Right-click the solution, and then click Build Solution.
  5. Right-click the SPGSharePointDataAccessQuickStart.SharePoint project, and then click Deploy.
  6. In the Web browser, navigate to http://{servername}/_layouts/SPG/SPGSharePointDataAccessQuickStart.aspx.

Using the List Data Access Interface

Figure 1 illustrates the List Data Access user interface.

Ff650062.List_QS_02(en-us,PandP.10).png

Figure 1
List data access interface

The page contains the following elements:

  • A drop-down box to select the list data access pattern
  • A link to refresh the page
  • A drop-down box that contains a list of the SPListItem IDs in the Announcements list
  • An Add button to add a new item to the Announcements list using the selected pattern
  • The title and description of the currently selected list item
  • A link to update the description of the currently selected item
  • A link to delete the currently selected item

You can experiment with this page to add, update, and delete items in the Announcements list.

To use the application

  1. In the Pattern drop-down box, click Direct, ListItemRepository, or AnnouncementRepository.
  2. Click Add to add a new announcement to the list.
  3. You see a new ID listed in the drop-down box.
  4. Select that new ID to show the new announcement.
  5. Click Update to update the description of the announcement.
  6. Click Delete to delete the announcement.

The application behaves similarly regardless of the pattern you select. However, when you examine the code paths with the Visual Studio debugger, you see that a different technique is used for each pattern.

Using the Visual Studio Debugger for a Code Walkthrough

The following section demonstrates how to use the Visual Studio debugger to understand the code.

To use the Visual Studio debugger

  1. In Visual Studio, in the SPGSharePointDataAccessQuickStart.SharePoint project, open the \Templates\Layouts\SPG\SPGSharePointDataAccessQuickStart.aspx.cs file and set a breakpoint at the start of the Page_Load method.
  2. In Visual Studio, click Attach to process on the Tools menu.
  3. Attach to the w3wp.exe process.
  4. In the user interface, add, update, or delete an announcement (for information, see the previous procedure). The debugger breaks at the first line of the Page_Load method.
  5. Use the F10 key (Step Over) and the F11 key (Step Into) to step through the code to see how the pattern you chose is implemented. You can also set additional breakpoints.

As you debug the Direct pattern, you will notice that it directly invokes the methods of the SPList class and the SPListItem class. Lists are identified with GUIDs and string constants.

In the List Item Repository pattern, calls to the SPList class and the SPListItem class are abstracted by the ListItemRepository class. This class insulates you from the details of directly invoking the SharePoint object model.

The Entity Repository pattern uses two layers to abstract the interaction with the SPListItem class. The first layer is a business entity abstraction that is provided by the Announcement class and by the AnnouncementRepository class. The second layer is provided by the ListItemRepository class. The business entity abstraction allows you to use an application-level Announcement data type as an argument to the Add, Update, and Delete operations.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Footer image

To provide feedback, get assistance, or download additional, please visit the SharePoint Guidance Community Web site.

Copyright © 2008 by Microsoft Corporation. All rights reserved.