Create SharePoint Online List Programmatically using PnP CSOM

This PnP SharePoint tutorial, we will discuss how to Create SharePoint Online List Programmatically using PnP CSOM, and then we will see how to create or add columns/fields in SharePoint Online list using PnP.

We will also check how to create a choice column in the SharePoint Online list using PnP CSOM and how to add a multi-choice field in the SharePoint Online list using PnP CSOM library.

If you are new to PnP, follow this article on PnP SharePoint Online Development using Patterns and Practices (PnP)

Create SharePoint Online List Programmatically using PnP

The below references we have to use in the code to work with PnP libraries:

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core

Here also I have created an asp.net application and added a button. Just click on the button, the list will be created with fields in SharePoint Online using the PnP core CSOM library.

<asp:Button ID="btnCreateListWithFields" runat="server" Text="Create List With Fields" OnClick="btnCreateListWithFields_Click" />
    <asp:Label ID="lblCreateListWithFields" runat="server" Text=""></asp:Label>

Below given snippet code creates a SharePoint Online list and fields in SharePoint online using PnP.

protected void btnCreateListWithFields_Click(object sender, EventArgs e)
        {
            CreateListWithFields();
        }
void CreateListWithFields()
 {
 try
 {
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{                                  
if (ctx.Web.ListExists("Employees"))
ctx.Web.GetListByTitle("Employees").DeleteObject();
ctx.ExecuteQueryRetry();
List employeesList = ListExtensions.CreateList(ctx.Web, ListTemplateType.GenericList, "Employees", false, false);
FieldCreationInformation fldEmpID = new FieldCreationInformation(FieldType.Number);
fldEmpID.DisplayName = "Employee ID";
fldEmpID.InternalName = "EmpID";
fldEmpID.AddToDefaultView = true;
fldEmpID.Id = Guid.NewGuid();
employeesList.CreateField(fldEmpID);
FieldCreationInformation fldEmpName = new FieldCreationInformation(FieldType.Text);
fldEmpName.DisplayName = "Employee Name";
fldEmpName.InternalName = "EmpName";
fldEmpName.AddToDefaultView = true;
fldEmpName.Id = Guid.NewGuid();
employeesList.CreateField(fldEmpName);
employeesList.Update();
ctx.ExecuteQueryRetry();
lblCreateListWithFields.Text = "Custom List with Fields Created Successfully";
 }
 }
catch (Exception ex)
 {
 }
 }

Run our application and click on the button “Create List With Fields” on the page.

create list programmatically in SharePoint
create list programmatically in SharePoint

Now our new SharePoint List “Employees” with few fields are created in the list.

create list programmatically in SharePoint using PnP core csom library
create list programmatically in SharePoint using PnP core csom library

In this SharePoint PnP article, we learned about how to create a list and add fields or columns using SharePoint PnP core CSOM Library.

Create a column in SharePoint Online list using PnP

Now, we will see how to create a field or column in the SharePoint Online list using the PnP core CSOM Library. We also know how to show/hide the fields in the NewForm or EditForm or DisplayForm of the sharepoint List using PnP.

Following functions are used for creating field in SharePoint List.

  • FieldExistsByName() is used to check the field exist in the SharePoint list or not.
  • SetShowInNewForm(), SetShowInEditForm() and SetShowInDisplayForm() functions are used to show or hide the field in the SharePoint list.

Here also I have created an asp.net application and added a button. Just click on the button, Field will create in the SharePoint Online List using the PnP core CSOM library.

<div>
<asp:Button ID="btnCreatefieldInCustomList" runat="server" Text="Create Field in Custom List" OnClick="btnCreatefieldInCustomList_Click" /><br />
<asp:Label ID="lblCreatefield" runat="server" Text=""></asp:Label>
</div>

Follow the below code for creating a field/column in the SharePoint List using PnP.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = "https://onlysharepoint2013.sharepoint.com/sites/TSInfoPNP";
string userName = "********@DOMAINAME.onmicrosoft.com";
string password = "*******";
protected void btnCreatefieldInCustomList_Click(object sender, EventArgs e)
{
    CreateFieldinTheList();
}
void CreateFieldinTheList()
{
string listName ="Order Details";
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
ctx.Load(ctx.Web);
ctx.ExecuteQueryRetry();
string displayName = "Order Number";
string internalName = "OrderNumber";	
FieldType fieldType = FieldType.Number;														
List olist = ctx.Web.GetListByTitle(listName);
bool listExists = ctx.Site.RootWeb.ListExists(listName);
if (listExists)
{
bool fieldexists = olist.FieldExistsByName(internalName);
if (!fieldexists)
{
FieldCreationInformation newFieldInfo = new FieldCreationInformation(fieldType);
newFieldInfo.DisplayName = displayName;
newFieldInfo.InternalName = internalName;
newFieldInfo.Id = Guid.NewGuid();
newFieldInfo.AddToDefaultView = true;	
			
Field ofield = olist.CreateField(newFieldInfo);
ofield.Description = "Product Order Number";
ofield.Required = true;						ofield.SetShowInNewForm(true);							ofield.SetShowInEditForm(false);							ofield.SetShowInDisplayForm(true);
ofield.Update();							ctx.ExecuteQueryRetry();
lblCreatefield.Text = "Field Created Successfully in the List";
}
else
{
lblCreatefield.Text = "Field is Already Exist in the List";
}
}
else
{
lblCreatefield.Text = "List Does not exists";
}										
}
}
catch (Exception ex) {
lblCreatefield.Text = ex.StackTrace;
//lblCreatefield.Text = "Problem in creating field in the list";
}
}

Run your application and click on button “Create Field in Custom List”.

SharePoint create field in the SharePoint list pro grammatically using PnP Core CSOM
SharePoint create field programmatically using PnP Core CSOM

Go to our SharePoint Online List and Now see the field is added in our SharePoint Online list.

Create Custom Field/Column in SharePoint Online List using PnP
Create Custom Field/Column in SharePoint Online List using PnP

Go to List Setting -> Click on Column/Field “Order Number” which we created now. In Edit Column, we can see the column/Field details.

Create Custom Field/Column in SharePoint Online List using PnP
Create Custom Field/Column in SharePoint Online List using PnP

Check the field/column is shown in the Edit Form it is hidden because we are setting the field/column in the code as “ofield.SetShowInEditForm(false);”.

Note: The field/column “Order Number” shown in the “QuickEdit” view Form only not like as in the EditForm in SharePoint list.

sharepoint online create field programmatically using PnP Core csom library
sharepoint online create field programmatically using PnP Core csom library

The same way you can check on NewForm and DisplayForm in SharePoint Online List.

Create Choice Field in SharePoint Online List using PnP CSOM

Now, let us see how to create a choice field in SharePoint online list using PnP CSOM.

The name of the choice column will be Status and it will have options like New, Pending, and Delivered in the SharePoint List name called “Order Details”.

The below references we have to use in the code to work with PnP libraries:

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core
  • OfficeDevPnP.Core.Entities

Following classes are used for creating Choice field/column in SharePoint Library.

  • FieldCreationInformation.
  • Field
  • FieldChoice

Following functions are used for creating Choice field/column in SharePoint Library.

  • CreateField() function used to create field in the SharePoint list.

Here I have created an asp.net application and I have added a button. Just click on the button, Choice Field(dropdown) will be created in SharePoint Online List using the PnP core CSOM library.

<div>
<asp:Button ID="btnCreateChoiceField" runat="server" Text="Create Choice Field In Custom List" OnClick="btnCreateChoiceField_Click" /><br />
<asp:Label ID="lblCreateChoiceField" runat="server" Text=""></asp:Label>
</div>

Below given snippet code create a choice field (dropdown) in SharePoint Online List using PnP.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = https://DomainName.sharepoint.com/sites/TSInfoPNP";
string userName = "*******@DomainName.onmicrosoft.com";
string password = "******";
protected void btnCreateChoiceField_Click(object sender, EventArgs e)
{
	Createchoicefield();
}
void Createchoicefield()
{

string listName = "Order Details";
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
ctx.Load(ctx.Web);
ctx.ExecuteQueryRetry();
List oList = ctx.Site.RootWeb.GetListByTitle(listName);
FieldCreationInformation choiceField = new FieldCreationInformation(FieldType.Choice);
choiceField.DisplayName = "Status";
choiceField.InternalName = "ProductOrderStatus";
choiceField.Id = Guid.NewGuid();
choiceField.AddToDefaultView = true;
choiceField.Required = true;
Field oField = oList.CreateField(choiceField);
FieldChoice fc = ctx.CastTo<FieldChoice>(oField);
List<string> options = new List<string>(fc.Choices);
options.Add("New");
options.Add("Pending");
options.Add("Delivered");
fc.Choices = options.ToArray();
// Update the choice field  
fc.Update();
ctx.ExecuteQueryRetry();
lblCreateChoiceField.Text = "Choice Field Created SuccesFully";
}
}
catch (Exception ex)
{
lblCreateChoiceField.Text = "Problem in createing Choice Field in the list";
}
}

Run the application and click on the button “Create Choice Field In Custom List” on the page.

create choice field in sharepoint list programmatically
create choice field in sharepoint list programmatically

Now the choice field (dropdown) is created in our SharePoint Online list. Go to the SharePoint list settings and scroll the page down to columns.

SharePoint create choice field programmatically using PnP core csom library
SharePoint create choice field programmatically using PnP core csom library

Click on Status” column and see the settings below created for the “Status” column.

create choice field in sharepoint list programmatically uisng pnp core csom library
create choice field in sharepoint list programmatically uisng pnp core csom library

Create Multi Choice Field in SharePoint Online List using PnP

Now we will see, how to create Multi choice Field in the SharePoint Online list using the PnP CSOM Library. A MultiChoice field or column allows users to select multiple options in the SharePoint List.

In this example, we will create a MultiChoice field/Column name “Technology” with options are like DotNet, SharePoint on-premises, and SharePoint Online in the SharePoint List name called “Employees”.

The following classes are used for creating the Choice field/column in the SharePoint Library.

  • FieldCreationInformation.
  • Field
  • FieldMultiChoice

Following functions are used for creating Choice field/column in SharePoint Library.

  • CreateField() function used to create field in the SharePoint list.

You can use the same asp.net application or you can add a new asp.net application. Just click on the button, MultiChoice Field(dropdown) is created in SharePoint Online List using the PnP core CSOM library.

<div>
<asp:Button ID="btnCreateMultiChoiceField" runat="server" Text="Create Multi Choice Field In The SharePoint List" OnClick="btnCreateMultiChoiceField_Click" />
<asp:Label ID="lblCreateMultiChoiceField" runat="server" Text=""></asp:Label>
</div>

Below given snippet code create a MultiChoice field(CheckBoxes) in SharePoint Online List using PnP.

AuthenticationManager authMgr = new AuthenticationManager();
string siteURL = https://DomainName.sharepoint.com/sites/TSInfoPNP";
string userName = "*******@DomainName.onmicrosoft.com";
string password = "******";
protected void btnCreateMultiChoiceField_Click(object sender, EventArgs e)
{
			CreateMultiChoicefield();
}

void CreateMultiChoicefield()
{
string listName = "Employees";
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
ctx.Load(ctx.Web);
ctx.ExecuteQueryRetry();
FieldCreationInformation choiceField = new FieldCreationInformation(FieldType.MultiChoice);
choiceField.DisplayName = "Technology";
choiceField.InternalName = "Technology";
choiceField.Id = Guid.NewGuid();
choiceField.AddToDefaultView = true;
choiceField.Required = true;
List oList = ctx.Site.RootWeb.GetListByTitle(listName);
Field oField = oList.CreateField(choiceField);
FieldMultiChoice fmc = ctx.CastTo<FieldMultiChoice>(oField);
List<string> options = new List<string>(fmc.Choices);
options.Add("DotNet");
options.Add("SharePoint OnPremises");
options.Add("Sharepoint Online");
fmc.Choices = options.ToArray();
// Update the choice field  
fmc.Update();
ctx.ExecuteQueryRetry();
lblCreateMultiChoiceField.Text = "Multi Choice Field Created SuccesFully";
}
}
catch (Exception ex)
{
lblCreateMultiChoiceField.Text = "Problem in createing Multi Choice Field in the list";
}
}

Run our application and click on the button “Create MultiChoice Field In SharePoint List” on the page.

sharepoint create multi choice field programmatically
sharepoint create multi choice field programmatically

Now the Multichoice field(CheckBoxes) is created in our SharePoint Online list. Go to our list ->list settings. Scroll down the page there click on new column name “Technology”.

sharepoint create multi choice field programmatically using PnP Core CSOM Library
sharepoint create multi choice field programmatically using PnP Core CSOM Library

See the Multi Choice Field (check boxes) is appeared in the SharePoint list.

sharepoint create multi choice field programmatically using PnP
sharepoint create multi choice field programmatically using PnP

Follow the below links create list in sharepoint Online/2019/2016/2013:

You may like following PnP SharePoint tutorials:

I hope this PnP SharePoint tutorial explains, how to create a SharePoint list using PnP and how to create or add columns to the SharePoint Online list using PnP. We also learned:

  • Create SharePoint Online List Programmatically using PnP
  • Create a column in SharePoint Online list using PnP
  • Create Choice Field in SharePoint Online List using PnP CSOM
  • Create Multi Choice Field in SharePoint Online List using PnP
>