Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:

Recycle Bin | MMS | ManagedVsCrawledProperties |

Tuesday, February 21, 2012

How to: Create a list in sharepoint programmatically

Before creating a list it’s better to check your newly created list already exists in the specified web or not and also if you are creating new list by using custom list template, we have to check if list template exists or not also.
Like similar ways, 

Here is the code to create a list in SharePoint programmatically
//To create a list
#region List Template
String ListTemplateName=”CustomListTemplateName”;
String ListName=”CustomListName”;
SPListTemplateCollection listTempColl = currentSite.GetCustomListTemplates(currentSite.RootWeb);
//checking if the list template exists or not before creating a list reason we are creating a custom list using custom template)
if (IsListTemplateExists(ListTemplateName, listTempColl) == true)
   //Creating the custom list using custom list template.
     SPListTemplate listQuickLinksTemp = listTempColl[ListTemplateName];
     CreateList(currentWeb, ListName, "Description of the list", listQuickLinksTemp);

#region CreateList
 /// FUNCTION        : CreateList
 /// PURPOSE         : To create the list
 /// PARAMETERS      : currentWeb -> current web
 /// strListName    -> A string containing the list name
 /// strlstDesc     -> A string containing the list description
 /// lstTemplate    -> A list template
 /// RETURNS         : None
 /// -----------------------------------------------------------------------
 public static bool CreateList(SPWeb currentWeb, string strListName, string strlstDesc, SPListTemplate lstTemplate)
    currentWeb.AllowUnsafeUpdates = true;
    //Checking list exists or not
    if (IsListExists(strListName, currentWeb) == true)
      return false;//list already exists
     //Create the list using template
       currentWeb.Lists.Add(strListName, strlstDesc, lstTemplate);                
       currentWeb.AllowUnsafeUpdates = false;
  catch (Exception ex)
  return true;
Please refer to get an idea about on what/why/where "AllowUnSafeUpdate".