共用方式為


SPWeb.GetList Method

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function GetList ( _
    strUrl As String _
) As SPList
'Usage
Dim instance As SPWeb
Dim strUrl As String
Dim returnValue As SPList

returnValue = instance.GetList(strUrl)
public SPList GetList(
    string strUrl
)

Parameters

  • strUrl
    Type: System.String

    The server-relative URL to the root folder of a list, such as /sites/sitecollection/subsite/Lists/Announcements.

Return Value

Type: Microsoft.SharePoint.SPList
The list at the specified server-relative URL.

Exceptions

Exception Condition
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.

Remarks

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.

Examples

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();
      }
   }
}

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

Other Resources

Describing Forms of URL Strings