SPWeb.GetListItemFields 方法

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

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Function GetListItemFields ( _
    strUrl As String, _
    ParamArray fields As String() _
) As SPListItem
用法
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
)

参数

  • strUrl
    类型:System.String

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

  • fields
    类型:[]

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

返回值

类型:Microsoft.SharePoint.SPListItem
The list item.

异常

异常 条件
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.

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

另请参阅

引用

SPWeb 类

SPWeb 成员

Microsoft.SharePoint 命名空间

GetListItem(String)