Just little about C#, .NET, SQL Server, SharePoint and SAP

Best way to check whether List exists

Posted by zieglers on January 17, 2011

I was trying to figure out the best way to get a list instance from SPWeb object and finally settled down to this one.

There are many different ways to get SPList in SharePoint Object Model, but guess what, there is no Exist() or ListExist() sort of method in object model. What a shame, eh? You can simply loop thru web.Lists collection but there is a price to pay of course..

Needless to say you can’t use it directly like this, web.Lists[“myListName”], since you can easily get an ArgumentException if list you are looking for doesn’t exist.

So, what to do.. Let LINQ handle it for you.. Here is my favourite so far..

web.Lists.Cast<SPList>().Any(mylist => mylist.Title == listName)


Have a static class in your Utility classes like this.

// Check if a list exists in a web
public static bool ListExists(SPWeb web, string listName){
   return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));

Note that you need .NET 3.5 to use this.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: