Test if a List exists
-
Monday, November 26, 2007 10:43 AMHello,
Is there another, simplier way to test if a list exists before using statements like :
Currently I always saw such statements inside try/catches to manage the case when the list doesn't exists.Code Blockweb.Lists["myList"]
Thank you for your answers.
Answers
-
Tuesday, November 27, 2007 1:41 AM
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
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 AMThank 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 PMI just ran into this issue myself. I find it odd that there isn't an Exists or Contains method on the Lists collection.

