SPWeb.GetListItemFields Method

Gets the list item that is associated with the specified server-relative URL, returning data for only the specified fields.

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

Syntax

'Declaration
Public Function GetListItemFields ( _
    strUrl As String, _
    ParamArray fields As String() _
) As SPListItem
'Usage
Dim instance As SPWeb
Dim strUrl As String
Dim fields As String()
Dim returnValue As SPListItem

returnValue = instance.GetListItemFields(strUrl, _
    fields)
public SPListItem GetListItemFields(
    string strUrl,
    params string[] fields
)

Parameters

  • strUrl
    Type: System.String

    The server-relative URL, such as "/sites/MySite/Shared Documents/MyDocument.docx".

  • fields
    Type: []

    The names of the fields for which values are returned in the list item.

Return Value

Type: Microsoft.SharePoint.SPListItem
The list item.

Exceptions

Exception Condition
ArgumentNullException

fields or strUrl is null .

Remarks

If fields is an empty array, this method does not return a value for any of the fields for the list item.

This method only gets the values for the specified fields in the list item. To get a list item based on a list item ID, use the GetItemByIdSelectedFields(Int32, []) method.

Examples

The following example is a console application that fetches a list item by specifying a URL and an array of field names. After the item is retrieved, the application prints the field values 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 server-relative Url for a list item.
            Dim itemUrl As String = web.RootFolder.ServerRelativeUrl
            itemUrl += "_catalogs/masterpage/default.master"

            ' Make a list of fields to fetch.
            Dim itemFields() As String = {"ContentTypeId", _
                                          "ContentType", _
                                          "HTML_x0020_File_x0020_Type", _
                                          "EncodedAbsUrl", _
                                          "FileLeafRef", _
                                          "UniqueId", _
                                          "FileSizeDisplay"}

            ' Get the list item.
            Dim item As SPListItem = web.GetListItemFields(itemUrl, itemFields)

            ' Print the values to the console.
            Dim fieldName As String
            For Each fieldName In itemFields
               Console.WriteLine("{0} = {1}", fieldName, item(fieldName))
            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 server-relative Url for a list item.
               string itemUrl = web.RootFolder.ServerRelativeUrl;
               itemUrl += "_catalogs/masterpage/default.master";

               // Make a list of fields to fetch.
               string[] itemFields = 
               {
                  "ContentTypeId",
                  "ContentType",
                  "HTML_x0020_File_x0020_Type",
                  "EncodedAbsUrl",
                  "FileLeafRef",
                  "UniqueId",
                  "FileSizeDisplay"
               };

               // Get the list item.
               SPListItem item = web.GetListItemFields(itemUrl, itemFields);

               // Print the values to the console.
               foreach (string fieldName in itemFields)
                  Console.WriteLine("{0} = {1}", fieldName, item[fieldName]);
            }
         }
         Console.ReadLine();
      }
   }
}
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text
Imports Microsoft.SharePoint

Module ConsoleApp

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

                If web.IsMultilingual Then
                    Dim sb As New StringBuilder()
                    Dim sep As String = ", "

                    Dim cultures As IEnumerable(Of CultureInfo) = web.SupportedUICultures

                    For Each culture As CultureInfo In cultures
                        sb.Append(culture.Name)
                        sb.Append(sep)
                    Next

                    Dim str As String = sb.ToString().Trim(sep.ToCharArray())
                    Console.WriteLine("Supported cultures: {0}", str)
                End If
                Console.WriteLine("Default culture: {0}", web.UICulture.Name)
            End Using
        End Using
        Console.WriteLine(vbCrLf + "Press ENTER to continue....")
        Console.Read()
    End Sub
End Module

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

GetListItem(String)