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 -

  1. Display Name

  2. Internal Name

  3. Field GUID

  4. 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 = "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 displayName = "TestColumn";          

  15. string internalName = "TestColumn";          

  16. string groupName = "TestPnPGroup";           

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

  18. FieldType fieldType = FieldType.Text;  

  19. // Field Creation Parameters         

  20. OfficeDevPnP.Core.Entities.FieldCreationInformation newFieldInfo = new OfficeDevPnP.Core.Entities.FieldCreationInformation(fieldType)          

  21. {              

  22. DisplayName = displayName,              

  23. InternalName = internalName,              

  24. Id = new Guid(fieldId),              

  25. Group = groupName          

  26. };  

  27. // Creates new Field         

  28. Field newField = clientContext.Site.RootWeb.CreateField(newFieldInfo);  

  29. // Output         

  30. Console.WriteLine("New " + newField.Title + " Column is created");          

  31. Console.WriteLine("Field ID : " + newField.Id);          

  32. Console.WriteLine("Default Value : " + newField.DefaultValue);          

  33. Console.WriteLine("Scope : " + newField.Scope);          

  34. Console.WriteLine("Schema XML : " + newField.SchemaXml);           

  35. Console.ReadKey();       

  36. }  

  37. }  

  38. catch (Exception ex)  

  39. {      

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

  41. Console.ReadKey();  

  42. }  

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. 

Recent Posts

See All