SPList.GetItems Method (SPView)
Returns a collection of items from the list based on the specified view.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Parameters
- view
- Type: Microsoft.SharePoint.SPView
The view to use in selecting list items.
| Exception | Condition |
|---|---|
| ArgumentNullException |
view is null (Nothing in Visual Basic). |
The following example is a console application that uses the "My Tasks" view of the Tasks list to retrieve list items. After fetching the data, the application prints a simple report to the console.
using System; using System.Collections.Specialized; using Microsoft.SharePoint; namespace Test { class ConsoleApp { static void Main(string[] args) { using (SPSite site = new SPSite("http://localhost")) { using (SPWeb web = site.OpenWeb()) { // Get data from a list. string listUrl = web.ServerRelativeUrl + "/lists/tasks"; SPList list = web.GetList(listUrl); SPView view = list.Views["My Tasks"]; SPListItemCollection items = list.GetItems(view); // Get a collection of view field names. StringCollection viewFields = view.ViewFields.ToStringCollection(); // Print data for each item in the view. foreach (SPListItem item in items) { // Print the value of each view field. foreach (string fieldName in viewFields) { Console.WriteLine("{0} = {1}", fieldName, item[fieldName]); } Console.WriteLine(); } } } Console.ReadLine(); } } }