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 -

  1. Content Type Name

  2. Field GUID

  3. Is required value

  4. 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 = "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 Parameters         

  14. string fieldIdStr = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";          

  15. string contentTypeName = "TestCT";  

  16. bool isRequired = false;  

  17. bool isHidden = false;  

  18. // Field As Guid         

  19. Guid fieldId = new Guid(fieldIdStr);  

  20. // Adds Field to Content Type using Name clientContext.Site.RootWeb.AddFieldToContentTypeByName(contentTypeName, fieldId, isRequired, isHidden);  

  21. // Output         

  22. Console.WriteLine("Field is added to Content Type");          

  23. Console.ReadKey();       

  24. }  

  25. }  

  26. catch (Exception ex)  

  27. {      

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

  29. Console.ReadKey();  

  30. }  

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 -

  1. Content Type Name

  2. 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 = "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 Parameters         

  14. string fieldName = "TestColumn";          

  15. string contentTypeName = "TestCT";  

  16. // Checks Field exists in CT bool fieldExists = clientContext.Site.RootWeb.FieldExistsByNameInContentType(contentTypeName, fieldName);  

  17. // Output

  18. if (fieldExists)         

  19.  {              

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

  21. }  

  22. else         

  23. {              

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

  25.  }          

  26. Console.ReadKey();       

  27. }  

  28. }  

  29. catch (Exception ex)  

  30. {      

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

  32. Console.ReadKey();  

  33. }  


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.

Recent Posts

See All