I have always faced this problem. While trying to access a list
through indexing, we normally use the following syntax.
SPSite site = new SPSite("url");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["ListName"];
The 3rd line normally throws an ArgumentException if the ListName is wrong
or if such a list doesn't exist. I also tried GetList() and GetListFromUrl()
or so, also to access a list. But all these methods throw exceptions like
File Not Found and so on. Is there a better way to access a List through
Object Model? Please advise. I always have to put the above piece of code in
a Try..Catch block and catch the argument exception and suppress it. I
googled it and found some implementation using extension methods. But there
also, they have the Try..Catch kind of implementation. Isn't there a more
elegant way to check whether a list exists (other than the above approach or
looping through all lists and then finding the matching instance)?