Uncategorized

SharePoint 2010: How to check whether Specified List Exists


The best way to write an Extension Method for SPWeb as shown below

public static class Extensions

 

   {

 

       public static bool ListExists(this SPWeb web, string listName)

 

       {

 

           return web.Lists.Cast<SPList>().Any(list => string.Compare(list.Title, listName, true) == 0);

 

       }

 

   }

 

This is an Extension method for SPweb and can be called as shown below

if (!web.ListExists("MyList"))

 

{

 

//do something

 

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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