PnP Combo Box List Item Picker In SharePoint Framework

PnP React Controls

 
Patterns and Practices (PnP) provides a list of reusable React controls to developers for building solutions such as webparts and extensions using SharePoint Framework.
 
Refer to this link to get the list of React controls for SPFx.
 
You will see how to use PnP Combo Box List Item Picker control in SPFx webpart.
 

PnP Combo Box List Item Picker control

 
This control allows you to select one or more items from a list. The List can be filtered to allow select items from a subset of items. The item selection is based from a column value. The control will suggest items based on the inserted value. Refer to this link for more details.
 
PnP Combo Box List Item Picker In SharePoint Framework
 
PnP Combo Box List Item Picker In SharePoint Framework
 
Note
I have created a custom list named Countries and added the items as shown below. Title field values will be considered as column value.
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
In this article, you will see how to perform the following tasks,
  • Prerequisites
  • Create SPFx solution
  • Implement Combo Box List Item Picker solution
  • Deploy the solution
  • Test the webpart
Prerequisites

Create SPFx solution

 
Open Node.js command prompt.
 
Create a new folder.
 
>md spfx-pnpreact-comboboxlistitempicker
 
Navigate to the folder.
 
> cd spfx-pnpreact-comboboxlistitempicker
 
Execute the following command to create SPFx webpart.
 
>yo @microsoft/sharepoint
 
Enter all the required details to create a new solution as shown below.
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
Yeoman generator will perform scaffolding process and once it is completed, lock down the version of project dependencies by executing the following command.
 
>npm shrinkwrap
 
Execute the following command to open the solution in the code editor.
 
>code .
 

Implement Combo Box List Item Picker solution

 
Execute the following command to install the PnP React Controls NPM package.
 
>npm install @pnp/spfx-controls-react --save
 
Open “src\webparts\pnPComboBoxListItemPicker\components\IPnPComboBoxListItemPickerProps.ts” file and update the code as shown below.
 
Note
List ID and column internal name are configurable in the webpart property pane that’s why we are adding in props.
  1. import {WebPartContext} from '@microsoft/sp-webpart-base';  
  2.   
  3. export interface IPnPComboBoxListItemPickerProps {  
  4.   listId: string;  
  5.   columnInternalName: string;  
  6.   context: WebPartContext;  
  7. }  
Open “src\webparts\pnPComboBoxListItemPicker\loc\mystrings.d.ts” file and update the code as shown below.
  1. declare interface IPnPComboBoxListItemPickerWebPartStrings {  
  2.   PropertyPaneDescription: string;  
  3.   BasicGroupName: string;  
  4.   ListIdFieldLabel: string;  
  5.   ColumnInternalNameFieldLabel: string;  
  6. }  
  7.   
  8. declare module 'PnPComboBoxListItemPickerWebPartStrings' {  
  9.   const strings: IPnPComboBoxListItemPickerWebPartStrings;  
  10.   export = strings;  
  11. }  
Open “src\webparts\pnPComboBoxListItemPicker\loc\en-us.js” file and update the code as shown below.
  1. define([], function () {  
  2.   return {  
  3.     "PropertyPaneDescription""Description",  
  4.     "BasicGroupName""Group Name",  
  5.     "ListIdFieldLabel""List ID",  
  6.     "ColumnInternalNameFieldLabel""Column Internal Name",  
  7.   }  
  8. });  
Open “src\webparts\pnPComboBoxListItemPicker\PnPComboBoxListItemPickerWebPart.manifest.json” file and update the following.
  • Update the interface
  • Update the render method
  • Update the getPropertyPaneConfiguration method
Update the interface:
  1. export interface IPnPComboBoxListItemPickerWebPartProps {  
  2.   listId: string;  
  3.   columnInternalName: string;  
  4. }  
Update the render method:
  1. public render(): void {  
  2.   const element: React.ReactElement<IPnPComboBoxListItemPickerProps> = React.createElement(  
  3.     PnPComboBoxListItemPicker,  
  4.     {  
  5.       listId: this.properties.listId,  
  6.       columnInternalName: this.properties.columnInternalName,  
  7.       context: this.context  
  8.     }  
  9.   );  
  10.   
  11.   ReactDom.render(element, this.domElement);  
  12. }  
Update the getPropertyPaneConfiguration method:
  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('listId', {  
  13.                  label: strings.ListIdFieldLabel  
  14.                }),  
  15.                PropertyPaneTextField('columnInternalName', {  
  16.                  label: strings.ColumnInternalNameFieldLabel  
  17.                })  
  18.              ]  
  19.            }  
  20.          ]  
  21.        }  
  22.      ]  
  23.    };  
Open “src\webparts\pnPComboBoxListItemPicker\components\PnPComboBoxListItemPicker.tsx” file and import the control.
  1. import { ComboBoxListItemPicker } from '@pnp/spfx-controls-react/lib/ComboBoxListItemPicker';  
Update the render method as shown below,
  1. public render(): React.ReactElement<IPnPComboBoxListItemPickerProps> {  
  2.     return (  
  3.       <div className={styles.pnPComboBoxListItemPicker}>  
  4.         <ComboBoxListItemPicker listId={this.props.listId}  
  5.           columnInternalName={this.props.columnInternalName}  
  6.           keyColumnInternalName='Id'  
  7.           onSelectedItem={this.onSelectedItem}  
  8.           webUrl={this.props.context.pageContext.web.absoluteUrl}  
  9.           multiSelect={true}  
  10.           spHttpClient={this.props.context.spHttpClient} />  
  11.       </div>  
  12.     );  
  13.   }  
onSelectedItem change event returns the selected folder,
  1. private onSelectedItem(data: { key: string; name: string }[]) {  
  2.   for (const item of data) {  
  3.     console.log(`Item value: ${item.key}`);  
  4.     console.log(`Item text: ${item.name}`);  
  5.   }  
  6. }  
Updated React component (src\webparts\pnPComboBoxListItemPicker\components\PnPComboBoxListItemPicker.tsx),
  1. import * as React from 'react';  
  2. import styles from './PnPComboBoxListItemPicker.module.scss';  
  3. import { IPnPComboBoxListItemPickerProps } from './IPnPComboBoxListItemPickerProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import { ComboBoxListItemPicker } from '@pnp/spfx-controls-react';  
  6.   
  7. export default class PnPComboBoxListItemPicker extends React.Component<IPnPComboBoxListItemPickerProps, {}> {  
  8.     
  9.   public render(): React.ReactElement<IPnPComboBoxListItemPickerProps> {  
  10.     return (  
  11.       <div className={styles.pnPComboBoxListItemPicker}>  
  12.         <ComboBoxListItemPicker listId={this.props.listId}  
  13.           columnInternalName={this.props.columnInternalName}  
  14.           keyColumnInternalName='Id'  
  15.           onSelectedItem={this.onSelectedItem}  
  16.           webUrl={this.props.context.pageContext.web.absoluteUrl}  
  17.           multiSelect={true}  
  18.           spHttpClient={this.props.context.spHttpClient} />  
  19.       </div>  
  20.     );  
  21.   }  
  22.   
  23.   private onSelectedItem(data: { key: string; name: string }[]) {  
  24.     for (const item of data) {  
  25.       console.log(`Item value: ${item.key}`);  
  26.       console.log(`Item text: ${item.name}`);  
  27.     }  
  28.   }  
  29. }  

Deploy the solution

 
Execute the following commands to bundle and package the solution.
 
>gulp bundle --ship
>gulp package-solution --ship
 
Navigate to tenant app catalog – Example: https://c986.sharepoint.com/sites/appcatalog/SitePages/Home.aspx
 
Upload the package file (sharepoint\solution\spfx-pnpreact-comboboxlistitempicker.sppkg).
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
Click Deploy.
 

Test the webpart

 
Navigate to the SharePoint site and add the app.
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
Navigate to the page and add the webpart as shown below.
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
Edit the webpart and update the properties.
 
PnP Combo Box List Item Picker In SharePoint Framework 
 
Result
 
PnP Combo Box List Item Picker In SharePoint Framework
 
PnP Combo Box List Item Picker In SharePoint Framework 
 

Summary

 
Thus, in this article, you saw how to use PnP Combo Box List Item Picker control in SharePoint Framework.