Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:

Recycle Bin | MMS | ManagedVsCrawledProperties |

Tuesday, February 21, 2012

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.
Refer Also:
  1. How to create a list programmatically?
  2. How to check if the lists exits ornot?
  3. How to check if the list templateexists or not?
  4. Error handling technique:

/// 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