top of page

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 = "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 = "PnPCSOMList";  

  15. // Retrieves list object using title         

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

  17. if (list != null)          

  18. {   // Displays required result             

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

  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. }  

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 = "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. Console.WriteLine("Get List By URL :");  

  14. // List Url Input

  15. string listUrl = "/Lists/PnPCSOMList";  

  16. // Gets list object using the list Url         

  17. List list = clientContext.Site.RootWeb.GetListByUrl(listUrl);  

  18. if (list != null)         

  19.  {  

  20. // Displays the list details             

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

  22. Console.WriteLine("List Id : " + list. Id);          

  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 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 = "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. Console.WriteLine("Get List Id By List Name ::");  

  14. // List Name

  15. string listName = "PnPCSOMList";  

  16. // Get List Id         

  17. Guid listId = clientContext.Site.RootWeb.GetListID(listName);           

  18. Console.WriteLine("List Id : " + listId);          

  19. Console.ReadKey();      

  20. }  

  21. }  

  22. catch (Exception ex)  

  23. {      

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

  25. Console.ReadKey();  

  26. }  

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.

0 comments
bottom of page