Add/Check Fields To/On SharePoint Online Content Types Using PnP Core CSOM Library

Introduction
 
In this article, you will learn about the basic field operations that can be performed on SharePoint Online content types, using PnP Core CSOM library. The following operations are explained.
  • Add Field to Content Type
  • Check if the field exists on Content Type
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 website here.
  • PnP Core CSOM library packages can be downloaded from 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 following method is used.
  • GetSharePointOnlineAuthenticatedContextToken 
The parameters required are.
  • SharePoint Online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
Note
 
The SharePoint fields can be viewed from site columns page. (https://siteurl/_layouts/15/mngfield.aspx)
The SharePoint content types can be viewed from the site content types page. (http://siteurl/_layouts/15/mngctype.aspx)
 
Add Field To Content Type
 
The required field can be added to the content type, using PnP Core CSOM library in multiple ways. In this approach, the content type can be added using content type name and field GUID. The following steps explain the process in detail. 
  • Input the site detail, user details for authentication, field and content type information.
  • Authenticate and get the client context of the site and then the necessary web object.
  • Using web object, add the field to content type using "AddFieldToContentTypeByName" method. The required parameters for the operation are -

    • Content Type Name
    • Field GUID
    • Is required value
    • To be hidden?

  • 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 Parameters  
  16.         string fieldIdStr = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";  
  17.         string contentTypeName = "TestCT";  
  18.         bool isRequired = false;  
  19.         bool isHidden = false;  
  20.   
  21.         // Field As Guid  
  22.         Guid fieldId = new Guid(fieldIdStr);  
  23.   
  24.         // Adds Field to Content Type using Name  
  25.         clientContext.Site.RootWeb.AddFieldToContentTypeByName(contentTypeName, fieldId, isRequired, isHidden);  
  26.   
  27.         // Output  
  28.         Console.WriteLine("Field is added to Content Type");  
  29.         Console.ReadKey();  
  30.   
  31.   
  32.     }  
  33. }  
  34. catch (Exception ex)  
  35. {  
  36.     Console.WriteLine("Error Message: " + ex.Message);  
  37.     Console.ReadKey();  
  38. }   
The following snapshot show the content type with added field.
 
 
Check if Field Exists in the Content Type
 
The field can be checked if it already exists on the content type, using PnP Core CSOM library. The following steps explain the process in detail. 
  • Input the site detail, user details for authentication, field and content type information.
  • Authenticate and get the client context of the site and then the necessary web object.
  • Using web object, check if the field exists on the content type, using "FieldExistsByNameInContentType" method. The required parameters for the operation are -

    • Content Type Name
    • Field Name

  • The return type of the method executed will be boolean (true/false).
  • 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 Parameters  
  16.         string fieldName = "TestColumn";  
  17.         string contentTypeName = "TestCT";  
  18.         // Checks Field exists in CT  
  19.         bool fieldExists = clientContext.Site.RootWeb.FieldExistsByNameInContentType(contentTypeName, 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.     }  
  35. }  
  36. catch (Exception ex)  
  37. {  
  38.     Console.WriteLine("Error Message: " + ex.Message);  
  39.     Console.ReadKey();  
  40. }  
Summary
 
Thus, you have learned the basic operations, like adding field to content type and checking if the field exists in the content type, using PnP Core CSOM library. The above samples are compatible on SharePoint Online sites. For SharePoint On-premise, the authentication method needs to be changed.