Monday, 15 July 2013

How to check if a list exists or not in SharePoint

It’s a very common programming practice to check our newly created list already exists in the specified web or not? Below code will help you on this scenario.

Before adding column(field) to the specified list it’s better to check whether that lists exists or not and new created field already exists or not? In my code, I am checking all these conditions.


/// 
/// FUNCTION    : IsListExists
/// PURPOSE     : Checking whether the List exists in the mentioned Web or not
/// PARAMETERS  : currentWeb -> current web
/// strListName-> A string containing the list name
/// RETURNS     : The result as a boolean
/// -----------------------------------------------------------------------
/// 
public static bool IsListExists(string strListName, SPWeb currentWeb)
 {
  bool isListExists = false;
  try {
    using (currentWeb)
    {
     isListExists = currentWeb.Lists.Cast().Any(list => string.Compare(list.Title, 
                              strListName, true) == 0);

    }
  }
  catch (Exception ex)
  {
    //Error hadling code
   }
 return isListExists;
}
//

No comments:

Post a Comment