SPWeb.GetList Method (Microsoft.SharePoint)
Returns the list that is associated with the specified server-relative URL.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

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

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

Parameters

strUrl

A string that contains the server-relative URL for a list, for example, /sites/MySiteCollection/Lists/Announcements.

Return Value

An SPList object that represents the list.
Remarks

When you call the GetList method using the full URL for a list that does not exist, a FileNotFoundException is thrown and the value of the AllowUnsafeUpdates property changes to false.

Use the GetListFromWebPartPageUrl method to return the list that is associated with the first Web Part in a Web Part page.

See Also

Tags :


Community Content

Thomas_Östreich
Url must be Server-relative

From my experience, the Url you pass to GetList must be server-relative, not site-relative . So the example above (/Lists/Announcements) will only work when you are in the root web of a web application that is mapped to the server root. This works for me:

 list = thisWeb.GetList(thisWeb.ServerRelativeUrl.TrimEnd('/') + "/Lists/Announcements")


thisweb.ServerRelativeUrl supplies a / when on the root site, so make sure to accomodate for that. GetList throws an exception when you call it with "//Lists/Announcements"

Steve Butler MSFT
Server-relative url with list name

If you need the List object and you only have a string with server-relative web url + list name. Then getting the list object is very simple.

SPList list = SPContext.Current.Site.RootWeb.GetList("/subsite1/subsite2/Shared Documents/");

Or in a console app...

SPSite site = new SPSite("http://myserver/");
SPList list = site.RootWeb.GetList("/subsite1/subsite2/Shared Documents/");

Kien Tran
Also works with absolute URL
I would like to point out that this method also works with an absolute URL (or full URL) to the list, as hinted in the "Remarks" section.

You don't have to extract a server-relative URL out of your absolute URL in order to use this method.
The following C# code will work:

SPList list = web.GetList("http://myserver/sites/SiteA/Lists/Projects/");
Tags :

Page view tracker