I have a url field that contains the url of a Discussion board that I will be creating a new item for. Thing is this discussion board can be in any site or web. So far I have two ideas but would like to know if there is a more proper way..

Option 1. String compare the given Url with the Url for every site saving the closest match. Then again using string compare on mySPSite.AllWebs to find the correct web, and finally use mySPWeb.GetListFromUrl(givenUrl) to grab my list object.

Option 2. Recursively remove the last segment of the url until it works

  private void myFunct(testURL){
     using(SPSite mysite = new SPSite(testUrl)){
        //vaild if reached
       int i = testURL.lastIndexOf('/');
         testURL= myFunct(testURL.subStr(0, i));
       }else{ testURL = null; }
    return testURL;

I would then open the SPSite and find the proper SPWeb in a simillar fashon

Both methods seem as though they would require many resouces.


You are on the right track, you would need to need to open the site, check to see if the URL is to a sub-web, and then look for the list. Here is one example (from http://techpunch.wordpress.com/2009/06/03/sharepoint-2007-get-splist-object-by-url/):

/// <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;

        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));
                    // 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;
  • 1
    Catch me if I'm wrong here, but wouldn't it be equally effective and slightly safer to use LastIndexOf() for finding /forms/ and /lists/? if someone has a subsite named forms or lists, which then contains forms or lists, wouldn't your solution break? and wouldn't my proposed switch fix that? Please enlighten me if I've missed something Nov 10 '15 at 19:22

Or the easier method of:

using (var site = new SPSite(url)){
  using (var web = site.OpenWeb()){
    var list = web.GetList(url);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.