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)
SharePoint Online tutorial contents:
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.
Now our new SharePoint List “Employees” with few fields are created in the list.
In this SharePoint PnP article, we learned about how to create a list and add fields or columns using SharePoint PnP core CSOM Library.
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”.
Go to our SharePoint Online List and Now see the field is added in our SharePoint Online list.
Go to List Setting -> Click on Column/Field “Order Number” which we created now. In Edit Column, we can see the column/Field details.
Check the field/column is shown in the Edit Form it is hidden because we are setting the field/column in the code as “
Note: The field/column “Order Number” shown in the “QuickEdit” view Form only not like as in the EditForm in SharePoint list.
The same way you can check on NewForm and DisplayForm in SharePoint Online List.
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.
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.
Click on
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.
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”.
See the Multi Choice Field (check boxes) is appeared in the SharePoint list.
Follow the below links create list in sharepoint Online/2019/2016/2013:
- How to create
list usingjsom (JavaScript object model) in SharePoint? - Create
list from list template in SharePoint Online 2013 programmatically - Create, Update and Delete SharePoint List using Rest API in SharePoint 2013/2016/Online
- Create List and respective fields programmatically Office 365 SharePoint 2013
You may like following PnP SharePoint tutorials:
- Create Folder and Subfolder in SharePoint Document Library using PnPÂ Core CSOM Library
- Delete All items from the SharePoint List using PnPÂ core CSOM Library Programmatically
- Rename or hide/remove SharePoint List Title Column Programmatically using PnP CSOM
- Create Modern Team Site and Subsite using pnp csom programmatically in SharePoint Online
- Create Subsite Programmatically using PnP in SharePoint Online
- How to Create a SharePoint list using PnP
- SharePoint Online copy list items to another list programmatically
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
I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site EnjoySharePoint.com