PnP List Item Picker in SharePoint Framework

Introduction

 
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 List Item Picker control in an SPFx webpart.
 

PnP List Item View 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 List Item Picker In SharePoint Framework
 
PnP 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 the column value.
 
PnP List Item Picker In SharePoint Framework 
 
In this article, you will see how to perform the following tasks:
  • Create an SPFx solution
  • Implement 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-listitempicker
 
Navigate to the folder.
 
> cd spfx-pnpreact-listitempicker
 
Execute the following command to create an SPFx webpart.
 
>yo @microsoft/sharepoint
 
Enter all the required details to create a new solution as shown below.
 
PnP 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 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\pnPListItemPicker\components\IPnPListItemPickerProps.ts” file and update the code as shown below.
 
Note
List ID, column internal name, and item limit is 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 IPnPListItemPickerProps {  
  4.   listId: string;  
  5.   columnInternalName: string;  
  6.   itemLimit: number;  
  7.   context: WebPartContext;  
  8. }  
Open “src\webparts\pnPListItemPicker\loc\mystrings.d.ts” file and update the code, as shown below.
  1. declare interface IPnPListItemPickerWebPartStrings {  
  2.   PropertyPaneDescription: string;  
  3.   BasicGroupName: string;  
  4.   ListIdFieldLabel: string;  
  5.   ColumnInternalNameFieldLabel: string;  
  6.   ItemLimitFieldLabel: string;  
  7. }  
  8.   
  9. declare module 'PnPListItemPickerWebPartStrings' {  
  10.   const strings: IPnPListItemPickerWebPartStrings;  
  11.   export = strings;  
  12. }  
Open “src\webparts\pnPListItemPicker\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.     "ItemLimitFieldLabel""Item Limit"  
  8.   }  
  9. });  
Open “src\webparts\pnPListItemPicker\PnPListItemPickerWebPart.ts” file and update the following.
  • Update the interface
  • Update the render method
  • Update the getPropertyPaneConfiguration method
Update the interface:
  1. export interface IPnPListItemPickerWebPartProps {  
  2.   listId: string;  
  3.   columnInternalName: string;  
  4.   itemLimit: number;  
  5. }  
Update the render method:
  1. public render(): void {  
  2.   const element: React.ReactElement<IPnPListItemPickerProps> = React.createElement(  
  3.     PnPListItemPicker,  
  4.     {  
  5.       listId: this.properties.listId,  
  6.       columnInternalName: this.properties.columnInternalName,  
  7.       itemLimit: this.properties.itemLimit,  
  8.       context: this.context  
  9.     }  
  10.   );  
  11.   
  12.   ReactDom.render(element, this.domElement);  
  13. }  
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.               PropertyPaneTextField('itemLimit', {  
  19.                 label: strings.ItemLimitFieldLabel  
  20.               })  
  21.             ]  
  22.           }  
  23.         ]  
  24.       }  
  25.     ]  
  26.   };  
  27. }  
Open “src\webparts\pnPListItemPicker\components\PnPListItemPicker.tsx” file and import the control.
  1. import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';  
Update the render method, as shown below.
  1. public render(): React.ReactElement<IPnPListItemPickerProps> {  
  2.   return (  
  3.     <div className={styles.pnPListItemPicker}>  
  4.       <ListItemPicker listId={this.props.listId}  
  5.         columnInternalName={this.props.columnInternalName}  
  6.         keyColumnInternalName='Id'  
  7.         itemLimit={this.props.itemLimit}  
  8.         onSelectedItem={this.onSelectedItem}  
  9.         context={this.props.context} />  
  10.     </div>  
  11.   );  
  12. }  
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\pnPListItemPicker\components\PnPListItemPicker.tsx),
  1. import * as React from 'react';  
  2. import styles from './PnPListItemPicker.module.scss';  
  3. import { IPnPListItemPickerProps } from './IPnPListItemPickerProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import { ListItemPicker } from '@pnp/spfx-controls-react/lib/listItemPicker';  
  6.   
  7. export default class PnPListItemPicker extends React.Component<IPnPListItemPickerProps, {}> {  
  8.   public render(): React.ReactElement<IPnPListItemPickerProps> {  
  9.     return (  
  10.       <div className={styles.pnPListItemPicker}>  
  11.         <ListItemPicker listId={this.props.listId}  
  12.           columnInternalName={this.props.columnInternalName}  
  13.           keyColumnInternalName='Id'  
  14.           itemLimit={this.props.itemLimit}  
  15.           onSelectedItem={this.onSelectedItem}  
  16.           context={this.props.context} />  
  17.       </div>  
  18.     );  
  19.   }  
  20.   private onSelectedItem(data: { key: string; name: string }[]) {  
  21.     for (const item of data) {  
  22.       console.log(`Item value: ${item.key}`);  
  23.       console.log(`Item text: ${item.name}`);  
  24.     }  
  25.   }  
  26. }  

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-listitempicker.sppkg). Click Deploy.
 
PnP List Item Picker In SharePoint Framework 
 

Test the webpart

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

Summary

 
In this article, you saw how to use the PnP List Item Picker control in SharePoint Framework.