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 = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // List name input

  14. string listName = "TestList";  

  15. // Checks the list exists

  16. bool listExists = clientContext.Site.RootWeb.ListExists(listName);  

  17. if (listExists)          

  18. {              

  19. Console.WriteLine("List is available on the site");          

  20. }  

  21. else        

  22.  {              

  23. Console.WriteLine("List is not available on the site");          

  24. }          

  25. Console.ReadKey();      

  26. }  

  27. }  

  28. catch (Exception ex)  

  29. {      

  30. Console.WriteLine("Error Message: " + ex.Message);      

  31. Console.ReadKey();  

  32. }  

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 = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // List Template         

  14. ListTemplateType listTemplate = ListTemplateType.GenericList;   // List Name

  15. string listName = "TestList";  

  16. // No versioning

  17. bool enableVersioning = false;  

  18. // Creates List         

  19. List list = clientContext.Site.RootWeb.CreateList(listTemplate, listName, enableVersioning);           

  20. Console.WriteLine("The list has been created.");          

  21. Console.WriteLine("List Name : " + list.Title);          

  22. Console.ReadKey();      

  23. }  

  24. }  

  25. catch (Exception ex)  

  26. {      

  27. Console.WriteLine("Error Message: " + ex.Message);      

  28. Console.ReadKey();  

  29. }  


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 = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // List name input

  14. string listName = "TestList";  

  15. // Get List         

  16. List list = clientContext.Site.RootWeb.GetListByTitle(listName);  

  17. // Set Permissions         

  18. list.SetListPermission(OfficeDevPnP.Core.Enums.BuiltInIdentity.Everyone, RoleType.Editor);       

  19.  Console.WriteLine("Provided permission");          

  20. Console.ReadKey();      

  21. }  

  22.  catch (Exception ex)  

  23. {      

  24. Console.WriteLine("Error Message: " + ex.Message);      

  25. Console.ReadKey();  

  26. }  


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 = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // List name input

  14. string listName = "TestList";  

  15. // Get List         

  16. List list = clientContext.Site.RootWeb.GetListByTitle(listName);  

  17. // Parameters for enabling versioning

  18. bool enableVersioning = true

  19. // Enable, false for disabling

  20. bool enableMinorVersioning = false;  

  21. bool updateQuery = true;  

  22. // Enables versioning         

  23. list.UpdateListVersioning(enableVersioning, enableMinorVersioning, updateQuery);           

  24. Console.WriteLine("Versioning enabled");          

  25. Console.ReadKey();      

  26. }  

  27. }  

  28. catch (Exception ex)  

  29. {      

  30. Console.WriteLine("Error Message: " + ex.Message);      

  31. Console.ReadKey();  

  32. }  


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. 

Recent Posts

See All