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

'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)

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.

ExceptionCondition
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 http://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("http://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
Show: