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. (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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string fieldIdStr = "5B0A8635-49A5-469F-8D42-1B92E8D4E17B";
- string contentTypeName = "TestCT";
- bool isRequired = false;
- bool isHidden = false;
-
-
- Guid fieldId = new Guid(fieldIdStr);
-
-
- clientContext.Site.RootWeb.AddFieldToContentTypeByName(contentTypeName, fieldId, isRequired, isHidden);
-
-
- 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.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string fieldName = "TestColumn";
- string contentTypeName = "TestCT";
-
- bool fieldExists = clientContext.Site.RootWeb.FieldExistsByNameInContentType(contentTypeName, fieldName);
-
-
- 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.