How to Create a SharePoint list using PnP

This PnP SharePoint tutorial explains, how to create a SharePoint list using PnP CSOM programmatically. We will also see how to enable few properties while creating the SharePoint list like EnableVersioning, OnQuickLaunch, etc.

The PnP code will also check if the list does not exist then it will create the list. If the SharePoint list exists, then it will not try to create the list.

If you are new to PnP, Following this article to know about PnP SharePoint Online Development using Patterns and Practices (PnP)

Create a SharePoint list using PnP

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

  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core

The following functions are used for creating a List in SharePoint Online using PnP SharePoint.

  • ListExists() is used to check the list is existing or not in SharePoint Online Site.
  • CreateList() is used to create a list in the SharePoint Online Site.

See the below function how it is working when creating a custom list in Sharepoint online site. You can find this line of code in the below example.

CreateList(ListTemplateType.GenericList,listName,true,true,"",true,false);
  • ListTemplateType.GenericList: it will create a generic list in our SharePoint Online Site.
  • listName: it is the name of our Sharepoint custom list.
  • First “true”: Enabled versioning in the SharePoint custom list(optional).
  • Second “true”: update and ExecuteQuery is set to true here(optional).
  • Third “true”: It is Enabled the Content-type for our Sharepoint Custom List.

Follow the below code it will create a custom list in your SharePoint Online Site.

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

void createList()
{
string listName = "Order Details";		
try
{
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteURL, userName, password))
{
ctx.Load(ctx.Web);
ctx.ExecuteQueryRetry();
bool listExists = ctx.Site.RootWeb.ListExists(listName);
if (!listExists)
{
							
List olist = ctx.Web.CreateList(ListTemplateType.GenericList,listName,true,true,"",true,false);
olist.Description = "List has Product order details";
olist.OnQuickLaunch = true;
//olist.EnableVersioning = true;
olist.Update();
ctx.Load(olist);
ctx.ExecuteQueryRetry();
lblCreateCustomList.Text = "List Created Successfully";
}
else
{
lblCreateCustomList.Text = "List already exists";
}
}	
}
catch (Exception ex)
{
lblCreateCustomList.Text = ex.StackTrace;
}
}

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

create sharepoint list using pnp
create sharepoint list using pnp

Now your sharepoint custom list created successfully in your site. Go to “Site Contents” in your SharePoint Online site.

create SharePoint list programmatically using PnP Core CSOM Library
create SharePoint list programmatically using PnP Core CSOM Library

Go to your SharePoint List Settings -> click on “List name, description, and navigation”.

In general settings Name, Description added and Navigation is enabled “Yes” for the Newly created SharePoint custom list.

create list programmatically in SharePoint PnP
create list programmatically in SharePoint

Go to List Settings-> click on “Versioning Settings”. Check versioning settings are enabled for newly created the SharePoint Custom List.

create SharePoint list programmatically using PnP Core CSOM Library
create SharePoint list programmatically using PnP Core CSOM Library

Go to List Settings -> click on “Advanced Settings”. Check the “Allow the management of Content-type” is enabled for Newly created the SharePoint Custom List.

create SharePoint list programmatically using PnP Core CSOM Library
create SharePoint list programmatically using PnP Core CSOM Library

Here Follow some of the links for creating Custom Lists in SharePoint Online:

You may like following PnP SharePoint tutorials:

In this article, we learned about how to check a list is existing or not in the SharePoint site. We also saw how to create a SharePoint list using PnP Core CSOM Library.

>