Export (0) Print
Expand All
Expand Minimize

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

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.

ExceptionCondition
ArgumentNullException

fields or strUrl is null .

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.

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.

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())
            {
               // 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("http://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
Show:
© 2014 Microsoft