SharePoint Online List Operations Using PnP Core CSOM Library - Part Two

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.
  1. // Input Parameters  
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context  
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context  
  12.     // Connects to SharePoint online site using inputs provided  
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         // List name input  
  16.         string listName = "TestList";  
  17.         // Checks the list exists  
  18.         bool listExists = clientContext.Site.RootWeb.ListExists(listName);  
  19.         if (listExists)  
  20.         {  
  21.             Console.WriteLine("List is available on the site");  
  22.         }  
  23.         else  
  24.         {  
  25.             Console.WriteLine("List is not available on the site");  
  26.         }  
  27.         Console.ReadKey();  
  28.     }  
  29. }  
  30. catch (Exception ex)  
  31. {  
  32.     Console.WriteLine("Error Message: " + ex.Message);  
  33.     Console.ReadKey();  
  34. }   
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.
  1. // Input Parameters  
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context  
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context  
  12.     // Connects to SharePoint online site using inputs provided  
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         // List Template  
  16.         ListTemplateType listTemplate = ListTemplateType.GenericList;  
  17.         // List Name  
  18.         string listName = "TestList";  
  19.         // No versioning  
  20.         bool enableVersioning = false;  
  21.         // Creates List  
  22.         List list = clientContext.Site.RootWeb.CreateList(listTemplate, listName, enableVersioning);  
  23.   
  24.         Console.WriteLine("The list has been created.");  
  25.         Console.WriteLine("List Name : " + list.Title);  
  26.         Console.ReadKey();  
  27.     }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31.     Console.WriteLine("Error Message: " + ex.Message);  
  32.     Console.ReadKey();  
  33. }   
Set List Permissions
 
The permissions can be granted to a list, using PnP Core CSOM library. The user or group will be provided the access with necessary permissions. The steps involved are,
  • Input the site detail, user details for authentication, and list name.
  • Authenticate and get the client context of the site.
  • Get the list instance, using list name.
  • Provide the permissions, using SetListPermission method using list object, user/group, permission type.
  • Output the result on the console.
The following code snippet shows the list permissions update operations, using PnP core CSOM. 
  1. // Input Parameters  
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context  
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context  
  12.     // Connects to SharePoint online site using inputs provided  
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         // List name input  
  16.         string listName = "TestList";  
  17.         // Get List  
  18.         List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  19.         // Set Permissions  
  20.         list.SetListPermission(OfficeDevPnP.Core.Enums.BuiltInIdentity.Everyone, RoleType.Editor);  
  21.   
  22.         Console.WriteLine("Provided permission");  
  23.         Console.ReadKey();  
  24.     }  
  25. }  
  26. catch (Exception ex)  
  27. {  
  28.     Console.WriteLine("Error Message: " + ex.Message);  
  29.     Console.ReadKey();  
  30. }   
Enable List Versioning
 
The versioning for the list can be enabled or disabled using the update operation on PnP Core CSOM library. The steps involved are,
  • Input the site detail, user details for authentication, and list name.
  • Authenticate and get the client context of the site.
  • Get the list instance, using list name.
  • Change the versioning using UpdateListVersioning method with necessary boolean values.
  • Output the result on the console.
The following code snippet shows the list version update operation, using PnP core CSOM.
  1. // Input Parameters  
  2. string siteUrl = "https://nakkeerann.sharepoint.com";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context  
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8.   
  9. try  
  10. {  
  11.     // Get and set the client context  
  12.     // Connects to SharePoint online site using inputs provided  
  13.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  14.     {  
  15.         // List name input  
  16.         string listName = "TestList";  
  17.         // Get List  
  18.         List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  19.         // Parameters for enabling versioning  
  20.         bool enableVersioning = true// Enable, false for disabling  
  21.         bool enableMinorVersioning = false;  
  22.         bool updateQuery = true;  
  23.         // Enables versioning  
  24.         list.UpdateListVersioning(enableVersioning, enableMinorVersioning, updateQuery);  
  25.   
  26.         Console.WriteLine("Versioning enabled");  
  27.         Console.ReadKey();  
  28.     }  
  29. }  
  30. catch (Exception ex)  
  31. {  
  32.     Console.WriteLine("Error Message: " + ex.Message);  
  33.     Console.ReadKey();  
  34. }  
Summary
 
Thus, you have learned how to perform basic list operations like creation, updating of permission/version, and checking if a list already exists, using PnP Core CSOM library. This article shows the basic operations. The complex logic can be built with the help of requirements. 

Up Next
    Ebook Download
    View all
    Learn
    View all