Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPWeb Class
SPWeb Methods
 GetList Method
Community Content
In this section
Statistics Annotations (3)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
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)
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.

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Url must be Server-relative      TeunD ... Thomas_Östreich   |   Edit   |   Show History

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"
Server-relative url with list name      Carsten Keutmann ... Steve Butler MSFT   |   Edit   |   Show History

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/");
Also works with absolute URL      Kien Tran   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker