Introduction
In this series of articles, you will learn all the list operations performed on the SharePoint online site, using PnP Core Client Side Object Model (CSOM) libraries.
This article is part two of the series. In this article, we will learn the following -
- How to check if the list exists
- Create a list,
- Set user permissions, and
- Enable versioning
In my previous article, I have explained about PnP Core CSOM library and how it can be imported to Visual Studio Console application solution to run the samples.
Let's get started.
Check if the list exists
ListExists method is used to check whether the SharePoint list is already available on the site on not. The steps involved are,
- Input the site details, user details for authentication, and list name
- Authenticate and get the client context of the site.
- Check if the list exists, by using ListExists method with the list name
- Output the result on the console.
The following code snippet shows the operation.
- // Input Parameters
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
- // PnP component to set context
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- // Get and set the client context
- // Connects to SharePoint online site using inputs provided
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- // List name input
- string listName = "TestList";
- // Checks the list exists
- bool listExists = clientContext.Site.RootWeb.ListExists(listName);
- if (listExists)
- {
- Console.WriteLine("List is available on the site");
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
Create List
The list can be created, using PnP Core CSOM library. Once the client context is set, a single line of query execution helps create a list, rather than a traditional CSOM approach. The steps involved are,
- Input the site detail, user details for authentication, list name, and list template.
- Authenticate and get the client context of the site.
- Create the list using CreateList method with above input parameters.
- Output the result on the console.
The following code snippet help create a list.
- // Input Parameters
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
- // PnP component to set context
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- // Get and set the client context
- // Connects to SharePoint online site using inputs provided
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- // List Template
- ListTemplateType listTemplate = ListTemplateType.GenericList;
- // List Name
- string listName = "TestList";
- // No versioning
- bool enableVersioning = false;
- // Creates List
- List list = clientContext.Site.RootWeb.CreateList(listTemplate, listName, enableVersioning);
- Console.WriteLine("The list has been created.");
- Console.WriteLine("List Name : " + list.Title);
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }

Tarun DPosted Sep 21, 2016, 2:33 AM
Good one..
Humayun Kabir MamunPosted Sep 21, 2016, 12:48 AM
Nice...
Prasanna MuraliPosted Sep 20, 2016, 9:36 PM
Nice share