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
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.
// Input Parameters
string siteUrl = "https://nakkeerann.sharepoint.com/";
string userName = "abc@nakkeerann.onmicrosoft.com";
string password = "***";
// PnP component to set context
AuthenticationManager authManager = new AuthenticationManager();
try
{
// Get and set the client context
// Connects to SharePoint online site using inputs provided
using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
// Input Parameters
string displayName = "TestColumn";
string internalName = "TestColumn";
string groupName = "TestPnPGroup";
string fieldId = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";
FieldType fieldType = FieldType.Text;
// Field Creation Parameters
OfficeDevPnP.Core.Entities.FieldCreationInformation newFieldInfo = new OfficeDevPnP.Core.Entities.FieldCreationInformation(fieldType)
{
DisplayName = displayName,
InternalName = internalName,
Id = new Guid(fieldId),
Group = groupName
};
// Creates new Field
Field newField = clientContext.Site.RootWeb.CreateField(newFieldInfo);
// Output
Console.WriteLine("New " + newField.Title + " Column is created");
Console.WriteLine("Field ID : " + newField.Id);
Console.WriteLine("Default Value : " + newField.DefaultValue);
Console.WriteLine("Scope : " + newField.Scope);
Console.WriteLine("Schema XML : " + newField.SchemaXml);
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error Message: " + ex.Message);
Console.ReadKey();
}
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.
Commentaires