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
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 -
Content Type Name
Field GUID
Is required value
To be hidden?
Display the results.
The following code snippet shows the logic.
// 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 fieldIdStr = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";
string contentTypeName = "TestCT";
bool isRequired = false;
bool isHidden = false;
// Field As Guid
Guid fieldId = new Guid(fieldIdStr);
// Adds Field to Content Type using Name clientContext.Site.RootWeb.AddFieldToContentTypeByName(contentTypeName, fieldId, isRequired, isHidden);
// Output
Console.WriteLine("Field is added to Content Type");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error Message: " + ex.Message);
Console.ReadKey();
}
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 -
Content Type Name
Field Name
The return type of the method executed will be boolean (true/false).
Display the results.
The following code snippet shows the logic.
// 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 fieldName = "TestColumn";
string contentTypeName = "TestCT";
// Checks Field exists in CT bool fieldExists = clientContext.Site.RootWeb.FieldExistsByNameInContentType(contentTypeName, fieldName);
// Output
if (fieldExists)
{
Console.WriteLine("Field is available");
}
else
{
Console.WriteLine("Field is not available");
}
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("Error Message: " + ex.Message);
Console.ReadKey();
}
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.
Comments