SPList.GetItems method (String[])

Returns a collection of items from the list but includes only the specified field values.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function GetItems ( _
    ParamArray fields As String() _
) As SPListItemCollection
'Usage
Dim instance As SPList
Dim fields As String()
Dim returnValue As SPListItemCollection

returnValue = instance.GetItems(fields)
public SPListItemCollection GetItems(
    params string[] fields
)

Parameters

  • fields
    Type: []

    A variable number of field names for which to get values. If all fields should be retrieved, use the Items property instead.

Return value

Type: Microsoft.SharePoint.SPListItemCollection
A collection of list items.

Exceptions

Exception Condition
ArgumentNullException

fields is null .

Remarks

This method creates a new SPQuery object from the specified fields and calls the GetItems(SPQuery) method with that new object to get the items.

This method is more efficient than accessing the Items property because it only fetches the values for the specified fields.

Examples

The following example is a console application that specifies field values to return in items retrieved from a list. After fetching the data, the application prints a simple report to the console.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("http://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 items As SPListItemCollection = list.GetItems("LinkTitle", "AssignedTo", "DueDate", "Status")

            ' Print a report header.
            Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}", _
                  "Assigned To", "Task", "Due Date", "Status")

            ' Print the details.
            Dim item As SPListItem
            For Each item In items
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}", _
                     item("AssignedTo"), item("LinkTitle"), item("DueDate"), item("Status"))
            Next item


         End Using
      End Using
      Console.ReadLine()
   End Sub
End Module
using System;
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);
               SPListItemCollection items = list.GetItems("LinkTitle", "AssignedTo", "DueDate", "Status");

               // Print a report header.
               Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                  "Assigned To", "Task", "Due Date", "Status");

               // Print the details.
               foreach (SPListItem item in items)
               {
                  Console.WriteLine("{0,-25}  {1,-20}  {2,-25}  {3}",
                     item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Status"]);
               }
            }
         }
         Console.ReadLine();
      }
   }
}

See also

Reference

SPList class

SPList members

GetItems overload

Microsoft.SharePoint namespace