Create SharePoint List/Document Library using C#
(How-To: Create a SharePoint list programmatically/Create a list/Add a Custom List to SharePoint/Programmatically create a custom SharePoint list):Overview:In one of my project, i am having requirements of list SharePoint List logging. For this, i required a SharePoint. I can create a SharePoint List by following way:
Create a SharePoint List by directly accessing the SharePoint Site
Create a SharePoint List with the help of SharePoint Object Model (
Microsoft.SharePoint namespace)
I adopted the second case in my windows application project.
Below are steps in detail.Step 1: Create a
xml file with name ListDetails.xml and copy following text in to that xml file:
Step 2: Create a .cs file and add following code:
private SPFieldType GetFieldType(string fieldType)
{
SPFieldType fieldTypeForColumn = SPFieldType.Text;
// Get the field type.
switch (fieldType)
{
case "Text":
fieldTypeForColumn = SPFieldType.Text;
break;
case "Choice":
fieldTypeForColumn = SPFieldType.Choice;
break;
case "Note":
fieldTypeForColumn = SPFieldType.Note;
break;
}
return fieldTypeForColumn;
}
private void AddFieldsToList(SPList referenceList, XmlNodeList columnsList)
{
// Variable Initialization.
SPField listField = null;
SPFieldType fieldType;
SPView listDefaultView = null;
SPFieldText fieldText = null;
try
{
foreach (XmlNode column in columnsList)
{
fieldType = GetFieldType(column.Attributes["DataType"].Value as string);
// Create the field
listField = referenceList.Fields.CreateNewField(fieldType.ToString(), column.Attributes["Name"].Value) as SPField;
// Set Various properties for the field.
listField.AllowDeletion = true;
listField.Description = column.Attributes["Description"].Value;
// Set the required properties for the column.
listField.ShowInDisplayForm = true;
listField.ShowInEditForm = true;
listField.ShowInListSettings = true;
listField.ShowInNewForm = true;
listField.ShowInVersionHistory = true;
referenceList.Fields.Add(listField);
// Add the column to the view of the list.
listDefaultView = referenceList.Views[0] as SPView;
listDefaultView.ViewFields.Add(column.Attributes["Name"].Value);
listDefaultView.Update();
referenceList.Update();
// Set the maximum length
if (fieldType.ToString().Equals(SPFieldType.Text.ToString(), StringComparison.InvariantCultureIgnoreCase) && column.Attributes["MaximumLength"] != null)
{
fieldText = referenceList.Fields[column.Attributes["Name"].Value] as SPFieldText;
fieldText.MaxLength = Convert.ToInt32(column.Attributes["MaximumLength"].Value);
fieldText.Update();
}
}
}
catch (Exception e)
{
}
}
public SPList CheckListExist(string strListName, SPWeb objWeb)
{
SPList objList = null;
try
{
//---Check list exist or not
if (objWeb.Lists[strListName] != null)
{
objList = objWeb.Lists[strListName] as SPList;
}
}
catch
{
objList = null;
}
return objList;
}
public SPList CreateReportList(string strXMLPath)
{
SPList objList = null;
XmlDocument xmlListDetailsDoc = new XmlDocument();
XmlNode objListNode = null;
XmlNodeList columnsList = null;
string urlSite = string.Empty;
string listName = string.Empty;
string description = string.Empty;
try
{
xmlListDetailsDoc.Load(strXMLPath);
// Get the URL
urlSite = xmlListDetailsDoc.SelectSingleNode("Lists").Attributes[0].Value;
// Get the list Node.
objListNode = xmlListDetailsDoc.SelectSingleNode("Lists/List");
// Process each list node and create/retrieve the list.
using (SPSite referenceSite = new SPSite(urlSite))
{
using (SPWeb referenceWeb = referenceSite.OpenWeb())
{
listName = objListNode.Attributes[0].Value;
description = objListNode.Attributes[1].Value;
if (!string.IsNullOrEmpty(listName))
{
//Check list exist or not
objList = CheckListExist(listName, referenceWeb);
if(objList==null)
{
//Create the list.
SPListTemplateType templateType = SPListTemplateType.GenericList;
Guid referenceListId = referenceWeb.Lists.Add(listName, description, templateType);
objList = referenceWeb.Lists[referenceListId];
// Set that the list can be displayed in quick launch.
objList.OnQuickLaunch = true;
// Create the columns in the list.
columnsList = objListNode.SelectNodes("Columns/Column");
if (columnsList != null && columnsList.Count > 0)
{
AddFieldsToList(objList, columnsList);
}
}
}
}
}
}
catch (Exception e)
{
objList = null;
}
return objList;
}
Step 3: Call the function CreateReportList(
) .
With the help of above mentioned steps you can create a SharePoint List using SharePoint Object Model.
Comments