SharePoint Online Create Field Operation Using PnP Core CSOM Library

Introduction
 
In this article, you will learn about creating SharePoint field/column operation performed on SharePoint Online, using PnP Core CSOM library.
 
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.
 
Prerequisite
  • PnP Core CSOM documentation can be found on the office 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.  
 
Create Field
 
The field can be created using PnP Core library. The steps involved are -
  • Input the site detail, user details for authentication, and field information.
  • Using field creation information object, the required parameters are set. The field type is passed while creating object. The parameters are -

    • Display Name
    • Internal Name
    • Field GUID
    • Group

  • Authenticate and get the client context of the site.
  • Create the field using "CreateField" method with necessary parameters.
The field GUID can be created from Visual Studio manually. The same can be obtained from Menu -> Tools -> Create GUID option.
 
  
 
The code snippet, given below, shows the field/column creation operation using PnP Core CSOM.
  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 displayName = "TestColumn";  
  17.         string internalName = "TestColumn";  
  18.         string groupName = "TestPnPGroup";  
  19.   
  20.         string fieldId = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";  
  21.         FieldType fieldType = FieldType.Text;  
  22.   
  23.         // Field Creation Parameters  
  24.         OfficeDevPnP.Core.Entities.FieldCreationInformation newFieldInfo = new OfficeDevPnP.Core.Entities.FieldCreationInformation(fieldType)  
  25.         {  
  26.             DisplayName = displayName,  
  27.             InternalName = internalName,  
  28.             Id = new Guid(fieldId),  
  29.             Group = groupName  
  30.         };  
  31.   
  32.         // Creates new Field  
  33.         Field newField = clientContext.Site.RootWeb.CreateField(newFieldInfo);  
  34.   
  35.         // Output  
  36.         Console.WriteLine("New " + newField.Title + " Column is created");  
  37.         Console.WriteLine("Field ID : " + newField.Id);  
  38.         Console.WriteLine("Default Value : " + newField.DefaultValue);  
  39.         Console.WriteLine("Scope : " + newField.Scope);  
  40.         Console.WriteLine("Schema XML : " + newField.SchemaXml);  
  41.   
  42.         Console.ReadKey();  
  43.   
  44.     }  
  45. }  
  46. catch (Exception ex)  
  47. {  
  48.     Console.WriteLine("Error Message: " + ex.Message);  
  49.     Console.ReadKey();  
  50. }   
The following snapshot shows the result displayed on the output console.
 
 
 
The field created can be viewed from the site columns page.


Summary 

Thus, you have learnt how to create SharePoint field or column on SharePoint site, using PnP Core CSOM library.