PnP Property Pane Controls For SharePoint Framework

In this article, we will explore various property pane controls offered by PnP that we can just install and start using in our solution.

Overview

 
Microsoft has provided various property pane controls for SharePoint Framework web parts. If our business demands, we can create our own custom property pane controls too. However, developing custom property pane controls is a bit of a tedious, time-consuming task. The amount of code that goes in for developing custom property pane controls is many-a-times equal to the amount of code being written for the actual web part. However, it also increases the maintenance burden. The code reusability in case of custom property pane controls is limited to copying the needed files across SPFx solutions and correcting the references. PnP (Patterns and Practices) has released a set of reusable property pane controls.
 

PnP Reusable Property Pane Controls

 
PnP (Office 365 Patterns and Practices) have released a set of reusable property pane controls that can be used in SPFx solutions. Below are few of commonly used controls offered from PnP:
  • Color picker
  • Date and time selector
  • List selector
  • People / group selector
  • Spin button
  • Managed metadata term selector
  • Multi-value selection
  • Number values
  • Information panel
In this example, we are going to use list selector property pane control in our SPFx web part.
 

Create SPFx Solution

 
Open the command prompt. Create a directory for SPFx solution.
  1. md pnp-list-selector-propertypane  
Navigate to the above-created directory.
  1. cd pnp-list-selector-propertypane  
Run the 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.
 
 
Solution Name: Hit Enter to have default name (pnp-list-selector-propertypane in this case) or type in any other name for your solution.
Selected choice: Hit Enter
 
Target for component: Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 or 2019 onwards).
Selected choice: SharePoint Online only (latest)

Place of files: We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Use the current folder

Deployment option: Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: Y

Permissions to access web APIs: Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)

Type of client-side component to create: We can choose to create client side webpart or an extension.
Selected choice: WebPart, since single part app pages are web parts.

Web Part Name: Hit Enter to select the default name or type in any other name.
Selected choice: ListSelector

Web part description: Hit Enter to select the default description or type in any other value.
Selected choice: Hit enter

Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected 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, install PnP property pane controls dependency by running below command.
  1. npm install @pnp/spfx-property-controls --save --save-exact  
Lock down the version of project dependencies by running below command.
  1. npm shrinkwrap  
In the command prompt, type below command to open the solution in the code editor of your choice.
  1. code .  

Use List Picker Control in Web Part

 
In order to use list picker property pane in SPFx web part, follow the below guidelines.
 
Open web part file “src\webparts\listSelector\ListSelectorWebPart.ts”.

Import the below modules.

  1. import { PropertyFieldListPicker, PropertyFieldListPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldListPicker';  

Create a new property for web part.

  1. export interface IListSelectorWebPartProps {  
  2.   description: string;  
  3.   lists: string | string[]; // Stores the list ID(s)  
  4. }  
Add custom property control to groupFields of web part property pane configuration.
  1. protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  2.   return {  
  3.     pages: [  
  4.       {  
  5.         header: {  
  6.           description: strings.PropertyPaneDescription  
  7.         },  
  8.         groups: [  
  9.           {  
  10.             groupName: strings.BasicGroupName,  
  11.             groupFields: [  
  12.               PropertyPaneTextField('description', {  
  13.                 label: strings.DescriptionFieldLabel  
  14.               }),  
  15.               PropertyFieldListPicker('lists', {  
  16.                 label: 'Select a list',  
  17.                 selectedList: this.properties.lists,  
  18.                 includeHidden: false,  
  19.                 orderBy: PropertyFieldListPickerOrderBy.Title,  
  20.                 disabled: false,  
  21.                 onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),  
  22.                 properties: this.properties,  
  23.                 context: this.context,  
  24.                 onGetErrorMessage: null,  
  25.                 deferredValidationTime: 0,  
  26.                 key: 'listPickerFieldId'  
  27.               })  
  28.             ]  
  29.           }  
  30.         ]  
  31.       }  
  32.     ]  
  33.   };  
  34. }  

Test the Web Part

   1. On the command prompt, type “gulp serve”.
   2. Open SharePoint site.
   3. Navigate to /_layouts/15/workbench.aspx.
   4. Add the web part to the page.
   5. Edit the property pane.

 

Summary

 
Reusable PnP controls are available as npm packages which can be easily added to an SPFx solution. It helps to make the code look simple and easy to maintain.