locked
Check to see if list exists RRS feed

  • Question

  • Hi - I need to deal with an exception if a list object doesnt exist e.g.

                if (site.Lists["My List"] == null)
                {
                    MessageBox.Show("No List Exists");
                }

    But this throws an 'Value does not fall within expected range" error - whats the elegant way to deal with these circumstances, I want to create a list or list item unless they already exist...

    Thanks a bunch.

    Macintos
    Friday, October 31, 2008 4:34 PM

Answers

  • Put a try/catch around it, and catch an IndexOutOfRangeException ...

    try 
    {  
     
    SPList list = site.Lists["My List"];  
     
    // do stuff  
     
    }  
    catch (IndexOutOfRangeException indEx)  
    {  
    // list does not exist  
    }  
    catch (Exception ex)  
    {  
    // other error thrown  

    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Friday, October 31, 2008 4:39 PM
  • Unfortunately, there is no way to check if an item exists in a collection in SharePoint without using try/catch.  I recommend using an extension method to add an Exists method to whatever you need.  It will help keep your code clean.  Here is an example.

    http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/03/using-an-extension-method-to-check-if-an-item-exists-in-a-sharepoint-collection.aspx

    Thanks,
    Corey
    Friday, October 31, 2008 7:13 PM

All replies

  • Put a try/catch around it, and catch an IndexOutOfRangeException ...

    try 
    {  
     
    SPList list = site.Lists["My List"];  
     
    // do stuff  
     
    }  
    catch (IndexOutOfRangeException indEx)  
    {  
    // list does not exist  
    }  
    catch (Exception ex)  
    {  
    // other error thrown  

    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Friday, October 31, 2008 4:39 PM
  • I tend to wrap it in a try, catch like so:

    SPList oList;
    try
    {
    oList = site.Lists["My list"];
    }
    catch (Exception oX)
    {
        //List does not exist - handle the value does not fall exception rather than every exception
    }

    if(oList != null)
    {
    //Perform actions when list is available etc etc
    }

    Stu
    Friday, October 31, 2008 4:42 PM
  • Unfortunately, there is no way to check if an item exists in a collection in SharePoint without using try/catch.  I recommend using an extension method to add an Exists method to whatever you need.  It will help keep your code clean.  Here is an example.

    http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/03/using-an-extension-method-to-check-if-an-item-exists-in-a-sharepoint-collection.aspx

    Thanks,
    Corey
    Friday, October 31, 2008 7:13 PM
  •  Maybe it's help you:

    Function:private static bool listExists(SPListCollection collection, string title)
    {
        foreach (SPList list in collection)
        {
           if (list.Title == title)
          {
            return true;
          }
        }
    return false;
    }
    ...
    Check exiting:
    ...
    if
    (listExists(web.Lists,  "ListTitle"))
    {
        ///Do something
    }
    Saturday, November 1, 2008 8:20 AM
  • You can try this using Linq:
    public static bool ListExists(SPWeb web, string listName)
    {
      return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));
    }
    
    or even also you can use it as an extension method:
    public static class Extensions {
      public static bool ListExists(this SPWeb web, string listName) {
        return web.Lists.Cast<SPList>().Any(l => l.Title.Equals(listName, StringComparison.OrdinalIgnoreCase));
      }
    }
    
    Make sure that you are using C# 3.0
    Regards,

    Ahmed Magdy
    • Proposed as answer by pjoyceAIS Wednesday, June 15, 2011 2:56 PM
    Wednesday, August 26, 2009 7:35 AM
  • The only following solution will help you. I have tried many ways but finally got it from the following.

    SPList NewList = null;

                NewList = MyWeb.Lists.TryGetList(ListName);

                if (null == NewList)
                {
                    MyWeb.AllowUnsafeUpdates = true;
                    Guid MyGuid = MyWeb.Lists.Add(.............);
                    MyWeb.AllowUnsafeUpdates = false;
                }


    Ravindra R Software Engineer OSI Consulting Email:rranabothu@osius.com

    Sunday, April 8, 2012 9:15 AM
  • How to check if SPList exists 
    public static bool ListExists(SPWeb web, string listUrl)
    {
       return web.Lists.Cast<SPList>().Any(list => string.Equals(list.RootFolder.ServerRelativeUrl, listUrl));
    }


    Vadim Gremyachev

    Thursday, February 6, 2014 12:02 AM