Thursday, May 28, 2009

Getting SPList from full URL of list

Code snippet for a quick and easy way of getting the SPList object from the full path to a SharePoint list:

String listurl = "http://sp:40006/Planets/Mars/Lists/Moonlist/AllItems.aspx";
SPList planetList;

using (SPSite sps = new SPSite(listurl))
{
using (SPWeb spw = sps.AllWebs[sps.ServerRelativeUrl])
{
planetList = spw.GetList(listurl);
}
};