Answered Test if a List exists

  • Monday, November 26, 2007 10:43 AM
     
     
    Hello,

    Is there another, simplier way to test if a list exists before using statements like :


    Code Block

    web.Lists["myList"]


    Currently I always saw such statements inside try/catches to manage the case when the list doesn't exists.

    Thank you for your answers.



Answers

  • Tuesday, November 27, 2007 1:41 AM
     
     Answered

    Unfortunately no - you have to wrap in a try/catch to get the ArgumentException.  I'd suggest just wrapping that code in a utility class.

All Replies

  • Tuesday, November 27, 2007 1:41 AM
     
     Answered

    Unfortunately no - you have to wrap in a try/catch to get the ArgumentException.  I'd suggest just wrapping that code in a utility class.

  • Tuesday, November 27, 2007 3:25 AM
     
     

    what about

    if (Web.Lists["listname"] == null)

    {
    List does not exist ....
    }

    Regards,
    Madhur
  • Tuesday, November 27, 2007 9:59 AM
     
     
    Thank you Gary for your answer.

    Madhur, the code you suggested will sadly always throw a "value is out of range" exception if the specified list doesn't exists before the test is done. Thus, it is not usable.
  • Thursday, October 02, 2008 2:07 PM
     
     
    I just ran into this issue myself.  I find it odd that there isn't an Exists or Contains method on the Lists collection.