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.
- // Input Parameters
- string siteUrl = "https://nakkeerann.sharepoint.com";
- string userName = "[email protected]";
- 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
- string listName = "TestList";
- // Get List
- List list = clientContext.Site.RootWeb.GetListByTitle(listName);
- // Input for creating view
- string viewName = "customview";
- string[] viewFields = { "Title", "Author" };
- bool defaultView = true;
- // Creates New View
- View newView = list.CreateView(viewName, ViewType.None, viewFields,30, defaultView, null, false, false);
- // Output
- Console.WriteLine("The view has been created.");
- Console.WriteLine("View Name : " + newView.Title);
- Console.WriteLine("View ID : "+newView.Id);
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }

Prasanna MuraliPosted Oct 9, 2016, 10:29 PM
Nice post...
Vignesh ManiPosted Oct 9, 2016, 6:06 PM
Nice one