Introduction
In this article, you will learn about the basic field operations that can be performed on SharePoint Online site, using PnP Core CSOM library. The following operations are explained.
- How to check if the field exists
- Retrieve the field
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 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.
Check if Field Exists
The required field can be checked if it's present on the site using PnP Core CSOM library. The following steps explain the process in detail.
- Input the site detail, user details for authentication, and field information.
- Authenticate and get the client context of the site and then the necessary web object.
- Using web object, check if the field exists using FieldExistsByName method. The required parameter for the method is field name.
- 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 = "Title1";
-
-
- bool fieldExists = clientContext.Site.RootWeb.FieldExistsByName(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();
- }
The results can be viewed on the console.
Retrieve Field from Site
The required field can be retrieved from the site using PnP Core CSOM library. The following steps explains the process in detail.
- Input the site detail, user details for authentication, and field information.
- Authenticate and get the client context of the site and then the necessary web object.
- Using web object, retrieve the field using GetFieldById method. The required parameter for the method is field GUID.
- Display the results.
The following sample code snippet shows the operations.
-
- 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))
- {
-
-
- Guid fieldId = new Guid("5B0A8635-49A5-469F-8D42-1B92E8D4E17B");
-
-
- Field field = clientContext.Site.RootWeb.GetFieldById<Field>(fieldId);
-
-
- Console.WriteLine("Field Name : " + field.Title);
- Console.WriteLine("Field Type : " + field.FieldTypeKind);
-
- Console.ReadKey();
-
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
The following snapshot shows the results.
Summary
Thus you have learned how to check/retrieve the fields on/from the SharePoint site using PnP Core CSOM library.