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.

  • SharePoint Online List Operations Using PnP Core CSOM Library - Part One

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

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. 

// Input Parameters 
string siteUrl = "https://nakkeerann.sharepoint.com";  
string userName = "abc@nakkeerann.onmicrosoft.com";  
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";  
 // Get List 
        List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
 // Set Permissions 
        list.SetListPermission(OfficeDevPnP.Core.Enums.BuiltInIdentity.Everyone, RoleType.Editor);  
 
        Console.WriteLine("Provided permission");  
        Console.ReadKey();  
    }  
}  
catch (Exception ex)  
{  
    Console.WriteLine("Error Message: " + ex.Message);  
    Console.ReadKey();  
}   

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.

// Input Parameters 
string siteUrl = "https://nakkeerann.sharepoint.com";  
string userName = "abc@nakkeerann.onmicrosoft.com";  
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";  
 // Get List 
        List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
 // Parameters for enabling versioning 
 bool enableVersioning = true; // Enable, false for disabling 
 bool enableMinorVersioning = false;  
 bool updateQuery = true;  
 // Enables versioning 
        list.UpdateListVersioning(enableVersioning, enableMinorVersioning, updateQuery);  
 
        Console.WriteLine("Versioning enabled");  
        Console.ReadKey();  
    }  
}  
catch (Exception ex)  
{  
    Console.WriteLine("Error Message: " + ex.Message);  
    Console.ReadKey();  
}  

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. 


Source: C# Corner

Recent Posts

See All