SharePoint Framework Extensions - Overview Of Field Customizer

Overview

SharePoint Framework (SPFx) Extensions are client-side components which allow us to extend the SharePoint user experience. Read more about SPFx extensions here.

In this article, we will explore the Field Customizer extension of SharePoint.

Brief information about Field Customizer

Field Customizer allows modifying the Views of the field in a list view. It can be used to override the field representation in the list. Field Customizer can be used with site columns or directly to the field inside a list.

In the classic SharePoint, we used to modify the representation of SharePoint list fields using JSLink property of the web part. In modern SharePoint, as JavaScript cannot be used on a page, the Field Customizer helps to implement these scenarios.

Create SPFx Solution

Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-extensions-fieldcustomizer  
Navigate to the above-created directory.
  1. cd spfx-extensions-fieldcustomizer  
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.

Yeoman generator
 
Solution Name
Hit Enter to have the default name (spfx-extensions-fieldcustomizer in this case) or type in any other name for your solution.
Preferred choice - Hit Enter
 
Target for component
Here, we can select the target environment where we are planning to deploy the client webpart; i.e. SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Preferred choice - SharePoint Online only (latest)
 
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Preferred choice - Same folder
 
Deployment option
Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Preferred choice - N (install on each site explicitly)
 
Type of client-side component to create
We can choose to create client side webpart or an extension.
Preferred choice - Extension
 
Type of client-side extension to create
We can choose to create Application customizer, Field customizer, or ListView Command Set.
 
Preferred Choice - Field Customizer
 
Field customizer name
Hit enter to select the default name or type in any other name.
 
Preferred choice - PercentFieldCustomizer
 
Field customizer description
Hit Enter to select the default description or type in any other value.
Preferred choice - Displays field in percentage
 
Framework to use
Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React).
Preferred choice - No JavaScript Framework
 
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.

Once the scaffolding process is completed, lock down the version of project dependencies by running below command
  1. npm shrinkwrap  
In the command prompt, type the below command to open the solution in a code editor of your choice.
  1. code .   
Solution Structure

The solution structure is similar to client-side web parts with similar configuration options.

Solution Structure

The file PercentFieldCustomizerFieldCustomizer.manifest.json inside the folder “\src\extensions\percentFieldCustomizer\” defines the extension type and unique identifier for the solution. Please note down the id. We will need it later for debugging purposes. 
  1. {  
  2.   "$schema""https://developer.microsoft.com/json-schemas/spfx/client-side-extension-manifest.schema.json",  
  3.   
  4.   "id""8db272d2-a083-48e7-b6da-dbf14fc98bbf",  
  5.   "alias""PercentFieldCustomizerFieldCustomizer",  
  6.   "componentType""Extension",  
  7.   "extensionType""FieldCustomizer",  
  8.   
  9.   // The "*" signifies that the version should be taken from the package.json  
  10.   "version""*",  
  11.   "manifestVersion": 2,  
  12.   
  13.   // If true, the component can only be installed on sites where Custom Script is allowed.  
  14.   // Components that allow authors to embed arbitrary script code should set this to true.  
  15.   // https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f  
  16.   "requiresCustomScript"false  
  17. }  
Implement Field Customizer

Open PercentFieldCustomizerFieldCustomizer.ts under “\src\extensions\percentFieldCustomizer\” folder. The class has below important methods,
  • onInit() event occurs before the page DOM is ready. It returns promise to perform asynchronous operations. onRenderCell() is not invoked until this promise is resolved.
  • onRenderCell() event occurs when each cell is rendered. It provides event.domElement to customize the representation of field.
  • onDisposeCell() can be used to free up any used resources that were allocated during rendering of the field to avoid any resource leak.
Debug Field Customizer

The SharePoint local workbench now cannot be used to test the field customizer as we will need an actual SharePoint site to create needed list and fields.
  1. Open SharePoint site
  2. From site contents create a new list named “Projects”

    Projects

  3. Click "Add column" to add a number field 
    Add column
  4. Name it Completion.

    Completion


  5. Click Save.
  6. Add some test data to the list.

    test data 
Update the Solution for Field changes
  1. Open serve.json file under config folder. Below is the default content.

    config folder

  2. Update the InternalFieldName attribute with the name of our field, i.e., Completion.
    1. {    
    2.   "$schema""https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",    
    3.   "port": 4321,    
    4.   "https"true,    
    5.   "serveConfigurations": {    
    6.     "default": {    
    7.       "pageUrl""https://contoso.sharepoint.com/sites/mySite/SitePages/myPage.aspx",    
    8.       "fieldCustomizers": {    
    9.         "Completion": {    
    10.           "id""8db272d2-a083-48e7-b6da-dbf14fc98bbf",    
    11.           "properties": {    
    12.             "sampleText""Value"    
    13.           }    
    14.         }    
    15.       }    
    16.     },    
    17.     "percentFieldCustomizer": {    
    18.       "pageUrl""https://contoso.sharepoint.com/sites/mySite/SitePages/myPage.aspx",    
    19.       "fieldCustomizers": {    
    20.         "Completion": {    
    21.           "id""8db272d2-a083-48e7-b6da-dbf14fc98bbf",    
    22.           "properties": {    
    23.             "sampleText""Value"    
    24.           }    
    25.         }    
    26.       }    
    27.     }    
    28.   }    
    29. }   
  3. In the command prompt, type the below command to run the extension.
    1. gulp serve   
  4. Accept loading of debug manifests by clicking “Load debug scripts”.

    Load debug scripts

  5. The list view should display the formatted Completion column as below.

     formatted Completion column 
Apply Field Customization
  1. Navigate back to the solution.

  2. Open PercentFieldCustomizerFieldCustomizer.module.scss file from “\src\extensions\percentFieldCustomizer\” folder and update the CSS.
    1. .PercentFieldCustomizer {    
    2.   .cell {    
    3.     background-color: "[theme:themePrimary, default:#e5e5e5]";    
    4.     display: 'inline-block';    
    5.   }    
    6.   .full {    
    7.     background-color: #e6e6e6;    
    8.     width: 100px;    
    9.   }    
    10. }    
  3. Open PercentFieldCustomizerFieldCustomizer.ts file from “\src\extensions\percentFieldCustomizer\” folder.

  4. Update onRenderCell method.
    1. public onRenderCell(event: IFieldCustomizerCellEventParameters): void {    
    2.     // Use this method to perform your custom cell rendering.    
    3.     event.domElement.classList.add(styles.cell);    
    4.     event.domElement.innerHTML = `    
    5.             <div class='${styles.PercentFieldCustomizer}'>    
    6.                 <div class='${styles.full}'>    
    7.                 <div style='width: ${event.fieldValue}px; background:#0094ff; color:#ffffff'>    
    8.                       ${event.fieldValue}    
    9.                 </div>    
    10.                 </div>    
    11.             </div>`;    
    12. }    
  5. Make sure the gulp server is running. Refresh the SharePoint list – Projects

    SharePoint list – Projects 
The completion field is now customized with the progress completion using field customizer.
 
Summary

Field Customizer SharePoint Framework extension helps to modify the representation of the field in SharePoint list. This is an alternative to JS Link in the modern SharePoint sites.