SPWeb.GetList 方法
Gets the list that is associated with the specified server-relative URL.
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Function GetList ( _
strUrl As String _
) As SPList
用法
Dim instance As SPWeb
Dim strUrl As String
Dim returnValue As SPList
returnValue = instance.GetList(strUrl)
public SPList GetList(
string strUrl
)
参数
strUrl
类型:System.StringThe server-relative URL to the root folder of a list, such as /sites/sitecollection/subsite/Lists/Announcements.
返回值
类型:Microsoft.SharePoint.SPList
The list at the specified server-relative URL.
异常
异常 | 条件 |
---|---|
FileNotFoundException | Either the URL does not specify a valid path to the website, or the list does not exist in the website. |
ArgumentException | The strUrl parameter is a null reference. |
备注
When you call this method using an invalid URL, an exception is thrown and the value of the AllowUnsafeUpdates property is set to false.
Use the GetListFromWebPartPageUrl method to return the list that is associated with the first Web Part in a Web Part page.
示例
The following example is a console application that demonstrates how to get an SPList object that represents the Announcements list on a website.
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("sitecollection/subsite")
Dim listUrl As String = "/sites/sitecollection/subsite/Lists/Announcements"
Dim list As SPList = web.GetList(listUrl)
Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl)
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("sitecollection/subsite"))
{
string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
SPList list = web.GetList(listUrl);
Console.WriteLine("List URL: {0}", list.RootFolder.ServerRelativeUrl);
}
}
Console.ReadLine();
}
}
}