Compartilhar via


SPList.GetItems Method (SPView)

Returns a collection of items from the list based on the specified view.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function GetItems ( _
    view As SPView _
) As SPListItemCollection
'Usage
Dim instance As SPList
Dim view As SPView
Dim returnValue As SPListItemCollection

returnValue = instance.GetItems(view)
public SPListItemCollection GetItems(
    SPView view
)

Parameters

Return Value

Type: Microsoft.SharePoint.SPListItemCollection
The list items specified by the view.

Exceptions

Exception Condition
ArgumentNullException

view is null (Nothing in Visual Basic).

Examples

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.

Imports System
Imports System.Collections.Specialized
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")
         Using web As SPWeb = site.OpenWeb()

            ' Get data from a list.
            Dim listUrl As String = web.ServerRelativeUrl + "/lists/tasks"
            Dim list As SPList = web.GetList(listUrl)
            Dim view As SPView = list.Views("My Tasks")
            Dim items As SPListItemCollection = list.GetItems(view)

            ' Get a collection of view field names.
            Dim viewFields As StringCollection = view.ViewFields.ToStringCollection()

            ' Print data for each item in the view.
            Dim item As SPListItem
            For Each item In items
               ' Print the value of each view field.
               Dim fieldName As String
               For Each fieldName In viewFields
                  Console.WriteLine("{0} = {1}", fieldName, item(fieldName))
               Next fieldName
               Console.WriteLine()
            Next item

         End Using
      End Using
      Console.ReadLine()
   End Sub
End Module
using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://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();
      }
   }
}

See Also

Reference

SPList Class

SPList Members

GetItems Overload

Microsoft.SharePoint Namespace

GetItems(String, [])