In SharePoint 2007, there was no method to check if a list exists or not, though it was a kind of basic requirement in any public API. The only way to check if list exists or not it as shown below. You needed to apply indexer to Web.Lists collection. If list existed then you would get the list either an exception would be thrown.
SPList myList = null;
try
{
myList = SPContext.Current.Web.Lists["mylist"];
}
catch { }
if(myList!=null)
{
//List exists
}
In SharePoint 2010, a new method TryGetList is added to check list existence. If the list doesn’t exists then the method returns null instead of throwing exception. However TryGetList takes list title but not list ID. It’s logical as if you have list guid then it might be true that you know the list exists (If the list doesn’t exist then how have you got the list id?).
SPList myList = SPContext.Current.Web.Lists.TryGetList('mylist');
if(myList!=null)
{
//List exists
}
Does this work for internal names as well as the display name?
ReplyDeleteUnfortunately there's no such method exits for internalname and display name. However, there's a method for static name TryGetFieldByStaticName
ReplyDelete