Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Returns a collection of items from the list based on the specified view.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
'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
)
view
Type: Microsoft.SharePoint.SPView
The view to use in selecting list items.
Type: Microsoft.SharePoint.SPListItemCollection
The list items specified by the view.
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.
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();
}
}
}
Microsoft.SharePoint namespace
GetItems(String, [])
Please sign in to use this experience.
Sign in