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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- string listName = "PnPCSOMList";
-
- List list = clientContext.Site.RootWeb.GetListByTitle(listName);
- if (list != null)
- {
-
- Console.WriteLine("List Title : " + list.Title);
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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-
-
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- Console.WriteLine("Get List By URL :");
-
-
- string listUrl = "/Lists/PnPCSOMList";
-
-
- List list = clientContext.Site.RootWeb.GetListByUrl(listUrl);
- if (list != null)
- {
-
- Console.WriteLine("List Title : " + list.Title);
- Console.WriteLine("List Id : " + list.Id);
- }
- else
- {
- Console.WriteLine("List is not available on the site");
- }
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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-
-
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
-
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
- Console.WriteLine("Get List Id By List Name ::");
-
-
- string listName = "PnPCSOMList";
-
- Guid listId = clientContext.Site.RootWeb.GetListID(listName);
-
- Console.WriteLine("List Id : " + listId);
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.