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

Introduction
 
In this article series, you will learn all the list operations performed on SharePoint Online, using PnP Core CSOM library. This article is part one of the list operations article series. In this article, you will learn, how to retrieve the lists from SharePoint online site by various methods, using PnP Core Client Side Object Model library.
 
The main advantage of using PnP Core libraries will be the reduced code to get the required information. The required object can be retrieved with just single line of code, once the client context is set.
 
This article series focuses on the operations related to SharePoint online sites. 
 
Prerequisite
 
PnP core CSOM documentation can be found on the office site here. PnP Core CSOM library packages can be downloaded here. The code, given below, is being tested, using Visual Studio Console Application. Once the Console Application is created, the packages can be installed, using Install-Package SharePointPnPCoreOnline command on package manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below-
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core 
Connect to SharePoint online site
 
The authentication manager is used to retrieve the client context of the site. To connect to SharePoint online site, the method, given below, is used-
  • GetSharePointOnlineAuthenticatedContextToken  
The parameters required are-
  • SharePoint online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
The following operations explains the list retrieval in detail. 
 
Retrieve List Using Title
 
The list can be retrieved by the name, using PnP Core library. The steps involved are- 
  • Input the site detail, user details for authentication and the list name.
  • Authenticate and get the client context of the site.
  • Get the list by using GetListByTitle method with the list name.
  • Output the required result on the console. 
The code snippet, given below, shows the list retrieval operation, using list title.
  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 = "PnPCSOMList";  
  17.         // Retrieves list object using title  
  18.         List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  19.         if (list != null)  
  20.         {  
  21.             // Displays required result  
  22.             Console.WriteLine("List Title : " + list.Title);  
  23.         }  
  24.         else  
  25.         {  
  26.             Console.WriteLine("List is not available on the site");  
  27.         }  
  28.         Console.ReadKey();  
  29.     }  
  30. }  
  31. catch (Exception ex)  
  32. {  
  33.     Console.WriteLine("Error Message: " + ex.Message);  
  34.     Console.ReadKey();  
  35. }   
Retrieve List Using URL
 
The list can be retrieved by the Server relative path of the list, using PnP Core library. The steps involved are-
  • Input the site detail, user details for an authentication and Server relative list URL.
  • Authenticate and get the client context of the site.
  • Get the list by using GetListByUrl method with list URL
  • Output the required result on the console.
The code snippet, given below, shows the list retrieval operation using list URL-
  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.         Console.WriteLine("Get List By URL :");  
  16.   
  17.         // List Url Input  
  18.         string listUrl = "/Lists/PnPCSOMList";  
  19.   
  20.         // Gets list object using the list Url  
  21.         List list = clientContext.Site.RootWeb.GetListByUrl(listUrl);  
  22.         if (list != null)  
  23.         {  
  24.             // Displays the list details  
  25.             Console.WriteLine("List Title : " + list.Title);  
  26.             Console.WriteLine("List Id : " + list.Id);  
  27.         }  
  28.         else  
  29.         {  
  30.             Console.WriteLine("List is not available on the site");  
  31.         }  
  32.         Console.ReadKey();  
  33.     }  
  34. }  
  35. catch (Exception ex)  
  36. {  
  37.     Console.WriteLine("Error Message: " + ex.Message);  
  38.     Console.ReadKey();  
  39. }   
Retrieve List Id
 
The required list Id can only be retrieved from the site using PnP Core library. The steps involved are, 
  • Input the site detail, user details for authentication and list title.
  • Authenticate and get the client context of the site.
  • Get the list, using GetListID method with list name
  • Output the required result on the console.
The code snippet, given below, shows the list Id retrieval 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.         Console.WriteLine("Get List Id By List Name ::");  
  16.   
  17.         // List Name  
  18.         string listName = "PnPCSOMList";  
  19.         // Get List Id  
  20.         Guid listId = clientContext.Site.RootWeb.GetListID(listName);  
  21.   
  22.         Console.WriteLine("List Id : " + listId);  
  23.         Console.ReadKey();  
  24.     }  
  25. }  
  26. catch (Exception ex)  
  27. {  
  28.     Console.WriteLine("Error Message: " + ex.Message);  
  29.     Console.ReadKey();  
  30. }   
Note To test the code, press F5 and wait for the console.
 
Summary
 
Thus, you have learned, how to retrieve the list information from the SharePoint site by various ways, using PnP Core CSOM library. The advantage of using this method is reduced complexity of the code.
 
In the next article, you will learn about other basic list operations available on PnP Core CSOM library like creating/updating list.