SPWeb.GetListItem method
SharePoint 2013
Gets the list item that is associated with the specified server-relative URL.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Parameters
- strUrl
- Type: System.String
The server-relative URL of the list item, such as "/sites/sitecollection/Shared Documents/MyDocument.docx", or an absolute URL, such as http://server/sites/sitecollection/Shared Documents/MyDocument.docx.
Return value
Type: Microsoft.SharePoint.SPListItemThe list item associated with the specified server-relative URL.
| Exception | Condition |
|---|---|
| ArgumentNullException | strUrl is null . |
| DirectoryNotFoundException | The URL does not specify a valid path. |
| FileNotFoundException | The URL does not point to a valid list item. |
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 http://localhost/sites/sitecollection and that this site collection has a website named subsite.
using System; using Microsoft.SharePoint; namespace Test { class ConsoleApp { static void Main(string[] args) { using (SPSite site = new SPSite("http://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(); } } }