In this example, we are going to use list selector property pane control in our SPFx web part.
Open the command prompt. Create a directory for SPFx solution.
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.
- npm install @pnp/spfx-property-controls --save --save-exact
Lock down the version of project dependencies by running below command.
In the command prompt, type below command to open the solution in the code editor of your choice.
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.
- import { PropertyFieldListPicker, PropertyFieldListPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldListPicker';
Create a new property for web part.
- export interface IListSelectorWebPartProps {
- description: string;
- lists: string | string[];
- }
Add custom property control to groupFields of web part property pane configuration.
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [
- {
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [
- {
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- }),
- PropertyFieldListPicker('lists', {
- label: 'Select a list',
- selectedList: this.properties.lists,
- includeHidden: false,
- orderBy: PropertyFieldListPickerOrderBy.Title,
- disabled: false,
- onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
- properties: this.properties,
- context: this.context,
- onGetErrorMessage: null,
- deferredValidationTime: 0,
- key: 'listPickerFieldId'
- })
- ]
- }
- ]
- }
- ]
- };
- }
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.