SharePoint 2007 – Get SPList Object by Url

I want to get a reference to a SPList object associated with a given list url.  This method works with any of the url’s associated with the list, including view page url’s, or list form url’s. 

This should be much easier to achieve by using the object model, in my opinion.  String parsing of the url is not my favorite, but I couldn’t find a better way to accomplish it. 

If you know of an easier way to accomplish this, please leave me a comment.  Otherwise, you can steal this from me, if you want it!

/// <summary>
/// Gets an SPList based on the url to the list
/// </summary>
/// <param name="listUrl">Full url to the list</param>
/// <returns>SPList object, null if list is not found</returns>
public SPList GetListByUrl(string listUrl)
{
    SPList list = null;

    try
    {
        using (SPSite site = new SPSite(listUrl))
        {
            if (site != null)
            {
                // Strip off the site url, leaving the rest
                // We'll use this to open the web
                string webUrl = listUrl.Substring(site.Url.Length);

                // Strip off anything after /forms/
                int formsPos = webUrl.IndexOf("/forms/", 0, StringComparison.InvariantCultureIgnoreCase);
                if (formsPos >= 0)
                {
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', formsPos));
                }

                // Strip off anything after /lists/
                int listPos = webUrl.IndexOf("/lists/", 0, StringComparison.InvariantCultureIgnoreCase);
                if (listPos >= 0)
                {
                    // Must be a custom list
                    // Strip off anything after /lists/
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', listPos));
                }
                else
                {
                    // No lists, must be a document library.
                    // Strip off the document library name
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/'));
                }

                // Get the web site
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    if (web != null)
                    {
                        // Initialize the web (avoids COM exceptions)
                        string title = web.Title;

                        // Strip off the relative list Url
                        // Form the full path to the list
                        //string relativeListUrl = listUrl.Substring(web.Url.Length);
                        //string url = SPUrlUtility.CombineUrl(web.Url, relativeListUrl);

                        // Get the list
                        list = web.GetList(listUrl);
                    }
                }
            }
        }
    }
    catch { }

    return list;
}

Enjoy!

  del.icio.us it!

digg it!

reddit!

technorati!

yahoo!

5 responses to “SharePoint 2007 – Get SPList Object by Url

  1. Jonathan Herschel

    Thanks! I was about to write the same block of code, now I can go get some grub….

  2. Nice one, Stuart. This is quite useful.

  3. Pingback: WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXX)! « Pasión por la tecnología…

  4. Pingback: WSS 3.0 & MOSS: Recopilatorio de enlaces interesantes (XXX)! - Blog del CIIN

  5. This will not work if the name of the list is something like:
    aaa – bbb
    It will resolve the dash out and you will never get the right name.

Leave a comment