다음을 통해 공유


SPList.GetItems Method (SPQuery)

Gets a collection of items from the list based on the specified query.

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

Syntax

'Declaration
<ClientCallableAttribute> _
<ClientCallableExceptionConstraintAttribute(FixedId := "e", ErrorCode := , Condition := "There is a join throttle failure.",  _
    ErrorType := GetType(SPQueryThrottledException))> _
<ClientCallableExceptionConstraintAttribute(FixedId := "d", ErrorCode := , Condition := "There is a throttle failure.",  _
    ErrorType := GetType(SPQueryThrottledException))> _
<ClientCallableExceptionConstraintAttribute(FixedId := "b", ErrorCode := , Condition := "The referenced field in the query hasn't been found.",  _
    ErrorType := GetType(SPException))> _
<ClientCallableExceptionConstraintAttribute(FixedId := "a", Condition := "Lack of permissions to perform the query.",  _
    ErrorType := GetType(UnauthorizedAccessException))> _
Public Function GetItems ( _
    query As SPQuery _
) As SPListItemCollection
'Usage
Dim instance As SPList
Dim query As SPQuery
Dim returnValue As SPListItemCollection

returnValue = instance.GetItems(query)
[ClientCallableAttribute]
[ClientCallableExceptionConstraintAttribute(FixedId = "e", ErrorCode = , Condition = "There is a join throttle failure.", 
    ErrorType = typeof(SPQueryThrottledException))]
[ClientCallableExceptionConstraintAttribute(FixedId = "d", ErrorCode = , Condition = "There is a throttle failure.", 
    ErrorType = typeof(SPQueryThrottledException))]
[ClientCallableExceptionConstraintAttribute(FixedId = "b", ErrorCode = , Condition = "The referenced field in the query hasn't been found.", 
    ErrorType = typeof(SPException))]
[ClientCallableExceptionConstraintAttribute(FixedId = "a", Condition = "Lack of permissions to perform the query.", 
    ErrorType = typeof(UnauthorizedAccessException))]
public SPListItemCollection GetItems(
    SPQuery query
)

Parameters

Return Value

Type: Microsoft.SharePoint.SPListItemCollection
The list items that satisfy the query.

Examples

The following example is a console application that gets a collection of items from the Tasks list and prints a report to the console.

Imports System
Imports Microsoft.SharePoint

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

            ' Build a query.
            Dim query As SPQuery = New SPQuery()
            query.Query = String.Concat( _
                              "<Where><Eq>", _
                                 "<FieldRef Name='Status'/>", _
                                 "<Value Type='CHOICE'>Not Started</Value>", _
                              "</Eq></Where>", _
                              "<OrderBy>", _
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />", _
                                 "<FieldRef Name='Priority' Ascending='TRUE' />", _
                              "</OrderBy>")

            query.ViewFields = String.Concat( _
                                   "<FieldRef Name='AssignedTo' />", _
                                   "<FieldRef Name='LinkTitle' />", _
                                   "<FieldRef Name='DueDate' />", _
                                   "<FieldRef Name='Priority' />")

            query.ViewFieldsOnly = True ' Fetch only the data that we need.

            ' 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(query)

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

            ' 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("Priority"))
            Next

         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("https://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Build a query.
               SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",
                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                              "</OrderBy>");                    

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.

               // Get data from a list.
               string listUrl = web.ServerRelativeUrl + "/lists/tasks";
               SPList list = web.GetList(listUrl);
               SPListItemCollection items = list.GetItems(query);

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

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

See Also

Reference

SPList Class

SPList Members

GetItems Overload

Microsoft.SharePoint Namespace

GetItems(String, [])