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,
- How to check if a list exists or not?
- How to check if a list template exists or not?
- How to delete list?
- Error handling technique:
//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); } #endregion #region CreateList ///Please refer to get an idea about on what/why/where "AllowUnSafeUpdate"./// 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) { try { currentWeb.AllowUnsafeUpdates = true; //Checking list exists or not if (IsListExists(strListName, currentWeb) == true) { return false;//list already exists } else { //Create the list using template currentWeb.Lists.Add(strListName, strlstDesc, lstTemplate); currentWeb.AllowUnsafeUpdates = false; } catch (Exception ex) { //Code } return true; } #endregion
Very good information available here thanks
ReplyDeleteAwsome post.Thanks
ReplyDelete