Export (0) Print
Expand All
Expand Minimize

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)

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.

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.

using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://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();
      }
   }
}
Show:
© 2014 Microsoft