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. (