top of page

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 = "abc@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context    

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. // Get and set the client context

  9. // Connects to SharePoint online site using inputs provided

  10. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  11. {  

  12. // Input Parameter         

  13. string fieldName = "Title1";  

  14. // Checks Field bool fieldExists = clientContext.Site.RootWeb.FieldExistsByName(fieldName);  

  15. // Output

  16. if (fieldExists)          

  17. {              

  18. Console.WriteLine("Field is available");          

  19. }  

  20. else         

  21. {              

  22. Console.WriteLine("Field is not available");          

  23. }          

  24. Console.ReadKey();      

  25.  }  

  26. }  

  27. catch (Exception ex)  

  28. {      

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

  30. Console.ReadKey();  

  31. }  

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 = "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. // Input Parameter         

  14. Guid fieldId = new Guid("5B0A8635-49A5-469F-8D42-1B92E8D4E17B");  

  15. // Retrieves Field         

  16. Field field = clientContext.Site.RootWeb.GetFieldById<Field>(fieldId);  

  17. // Output         

  18. Console.WriteLine("Field Name : " + field.Title);          

  19. Console.WriteLine("Field Type : " + field.FieldTypeKind);           

  20. Console.ReadKey();       

  21. }  

  22. }  

  23. catch (Exception ex)  

  24. {      

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

  26. Console.ReadKey();  

  27. }    

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. 

0 comments

Commenti


bottom of page