SPWeb.GetListItem 方法

Gets the list item that is associated with the specified server-relative URL.

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

语法

声明
Public Function GetListItem ( _
    strUrl As String _
) As SPListItem
用法
Dim instance As SPWeb
Dim strUrl As String
Dim returnValue As SPListItem

returnValue = instance.GetListItem(strUrl)
public SPListItem GetListItem(
    string strUrl
)

参数

  • strUrl
    类型:System.String

    The server-relative URL of the list item, such as "/sites/sitecollection/Shared Documents/MyDocument.docx", or an absolute URL, such as https://server/sites/sitecollection/Shared Documents/MyDocument.docx.

返回值

类型:Microsoft.SharePoint.SPListItem
The list item associated with the specified server-relative URL.

异常

异常 条件
ArgumentNullException

strUrl is null .

DirectoryNotFoundException

The URL does not specify a valid path.

FileNotFoundException

The URL does not point to a valid list item.

备注

This method returns null if the list item cannot be found.

示例

The following example is a console application that retrieves a list item from a document library and then prints the name of the associated file to the console.

Note that the example assumes the existence of a site collection with an absolute URL of https://localhost/sites/sitecollection and that this site collection has a website named subsite.

Imports System
Imports Microsoft.SharePoint

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

            ' Build a server-relative Url for a list item.
            Dim itemUrl As String = web.RootFolder.ServerRelativeUrl
            itemUrl += "_catalogs/masterpage/default.master"

            ' Get the list item.
            Dim item As SPListItem = web.GetListItem(itemUrl)

            ' Print the file name.
            Console.WriteLine(item.File.Name)

         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/sites/sitecollection"))
         {
            using (SPWeb web = site.OpenWeb("subsite"))
            {
               // Build a server-relative Url for a list item.
               string itemUrl = web.RootFolder.ServerRelativeUrl;
               itemUrl += "_catalogs/masterpage/default.master";

               // Get the list item.
               SPListItem item = web.GetListItem(itemUrl);

               // Print the file name.
               Console.WriteLine(item.File.Name);
            }
         }
         Console.ReadLine();
      }
   }
}

另请参阅

引用

SPWeb 类

SPWeb 成员

Microsoft.SharePoint 命名空间