SharePoint Online List View Operations, Using PnP Core CSOM Library

Introduction
 
In this article, you will learn about list view operations performed on SharePoint Online, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get the required information. The required object can be retrieved with a very small piece of code, once the client context is set.
 
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 below method is used.
  • GetSharePointOnlineAuthenticatedContextToken 
The parameters required are -
  • SharePoint Online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
The following operations explain the list view methods in details.
 
Create List View
 
The list view can be created using PnP Core library. The steps involved are -
  • Input the site detail, user details for authentication, existing list name, and new view name.
  • Authenticate and get the client context of the site.
  • Get the list by using GetListByTitle method with the list name.
  • Then, create view by passing the input parameters like View name, View type, View fields, row limit, query. 
  • Output the required result on the console. 
The code snippet, given below, shows the list view creation operation, using existing list. 
  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  
  16.         string listName = "TestList";  
  17.   
  18.         // Get List  
  19.         List list = clientContext.Site.RootWeb.GetListByTitle(listName);  
  20.   
  21.         // Input for creating view  
  22.         string viewName = "customview";  
  23.         string[] viewFields = { "Title""Author" };  
  24.         bool defaultView = true;  
  25.   
  26.         // Creates New View  
  27.         View newView = list.CreateView(viewName, ViewType.None, viewFields,30, defaultView, null, falsefalse);  
  28.   
  29.         // Output  
  30.         Console.WriteLine("The view has been created.");  
  31.         Console.WriteLine("View Name : " + newView.Title);  
  32.         Console.WriteLine("View ID : "+newView.Id);  
  33.         Console.ReadKey();  
  34.     }  
  35. }  
  36. catch (Exception ex)  
  37. {  
  38.     Console.WriteLine("Error Message: " + ex.Message);  
  39.     Console.ReadKey();  
  40. }   
Retrieve List View
 
The list view can be retrieved using two ways with PnP Core library - by name and by Id.
 
By Name
 
The list view can be retrieved by the view name, using PnP Core library. The steps involved are -
  • Input the site detail, user details for an authentication, list name and view name.
  • Authenticate and get the client context of the site.
  • Get the list by using list retrieval method.
  • Then, retrieve the view by view name. 
  • Output the required result on the console. 
The code snippet, given below, shows the list retrieval operation using list view name. 
  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. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         // List Name  
  16.         string listName = "TestList";  
  17.         // View Name  
  18.         string viewName = "customview";  
  19.   
  20.         // Get View By Name  
  21.         View view = clientContext.Site.RootWeb.GetListByTitle(listName).GetViewByName(viewName);  
  22.   
  23.   
  24.         // Output  
  25.         Console.WriteLine("View Url  : " + view.ServerRelativeUrl);  
  26.         Console.ReadKey();  
  27.     }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31.     Console.WriteLine("Error Message: " + ex.Message);  
  32.     Console.ReadKey();  
  33. }  
By Id
 
The list view can be retrieved by the view Id, using PnP Core library. The steps involved are, 
  • Input the site detail, user details for an authentication, list name and view name.
  • Authenticate and get the client context of the site.
  • Get the list by using list retrieval method and then retrieve the view object by view Id.
  • Output the required result on the console. 
The code snippet, given below, shows the list view retrieval operation using list view Id. 
  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. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         // List Name  
  16.         string listName = "TestList";  
  17.         // View Id  
  18.         Guid viewId = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");  
  19.   
  20.         // Get View By Id  
  21.         View view = clientContext.Site.RootWeb.GetListByTitle(listName).GetViewById(viewId);  
  22.           
  23.         // Output  
  24.         Console.WriteLine("View Name : " + view.Title);  
  25.         Console.WriteLine("View Url  : " + view.ServerRelativeUrl);  
  26.         Console.WriteLine("View Id   : " + view.Id);  
  27.         Console.ReadKey();  
  28.     }  
  29. }  
  30. catch (Exception ex)  
  31. {  
  32.     Console.WriteLine("Error Message: " + ex.Message);  
  33.     Console.ReadKey();  
  34. }   
Note - To test the code, press F5 and wait for the console. 
 
Summary
 
Thus, you have learned how to add and retrieve list view to/from the SharePoint Online list, using PnP Core CSOM Component library. 

Up Next
    Ebook Download
    View all
    Learn
    View all