Check/Retrieve Field Operations On SharePoint Online Using PnP Core CSOM Library

Introduction
 
In this article, you will learn about the basic field operations that can be performed on SharePoint Online site, using PnP Core CSOM library. The following operations are explained.
  • How to check if the field exists
  • Retrieve the field 
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.
 
Note The field is known as columns in the SharePoint list. 
 
Prerequisite
  • PnP Core CSOM documentation can be found on the official 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 SharePoint fields can be viewed from site columns page. https://siteurl/_layouts/15/mngfield.aspx.
 
Check if Field Exists
 
The required field can be checked if it's present on the site using PnP Core CSOM library. The following steps explain the process in detail. 
  • Input the site detail, user details for authentication, and field information.
  • Authenticate and get the client context of the site and then the necessary web object. 
  • Using web object, check if the field exists using FieldExistsByName method. The required parameter for the method is field name.
  • Display the results. 
The following code snippet shows the logic.
  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.   
  15.         // Input Parameter  
  16.         string fieldName = "Title1";  
  17.   
  18.         // Checks Field  
  19.         bool fieldExists = clientContext.Site.RootWeb.FieldExistsByName(fieldName);  
  20.   
  21.         // Output  
  22.         if (fieldExists)  
  23.         {  
  24.             Console.WriteLine("Field is available");  
  25.         }  
  26.         else  
  27.         {  
  28.             Console.WriteLine("Field is not available");  
  29.         }  
  30.         Console.ReadKey();  
  31.   
  32.     }  
  33. }  
  34. catch (Exception ex)  
  35. {  
  36.     Console.WriteLine("Error Message: " + ex.Message);  
  37.     Console.ReadKey();  
  38. }  
The results can be viewed on the console. 
 
Retrieve Field from Site
 
The required field can be retrieved from the site using PnP Core CSOM library. The following steps explains the process in detail. 
  • Input the site detail, user details for authentication, and field information.
  • Authenticate and get the client context of the site and then the necessary web object.
  • Using web object, retrieve the field using GetFieldById method. The required parameter for the method is field GUID.
  • Display the results. 
The following sample code snippet shows the operations.
  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.   
  15.         // Input Parameter  
  16.         Guid fieldId = new Guid("5B0A8635-49A5-469F-8D42-1B92E8D4E17B");  
  17.   
  18.         // Retrieves Field  
  19.         Field field = clientContext.Site.RootWeb.GetFieldById<Field>(fieldId);  
  20.   
  21.         // Output  
  22.         Console.WriteLine("Field Name : " + field.Title);  
  23.         Console.WriteLine("Field Type : " + field.FieldTypeKind);  
  24.           
  25.         Console.ReadKey();  
  26.   
  27.     }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31.     Console.WriteLine("Error Message: " + ex.Message);  
  32.     Console.ReadKey();  
  33. }  
 The following snapshot shows the results.
 
  
 
Summary
 
Thus you have learned how to check/retrieve the fields on/from the SharePoint site using PnP Core CSOM library. 

Up Next
    Ebook Download
    View all
    Learn
    View all