My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

Create SharePoint List/Document Library using programming

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

Popular posts from this blog

Sitecore GraphQL Queries

Twenty 20 Masti:

Sitecore Experience Edge GraphQL Queries