PnP ListPicker Control For SPFx

In this article, you will learn about PnP ListPicker Control for SPFx.

Overview

 
Many times, the SharePoint web part needs to show information from the SharePoint site. One good example of it is to show SharePoint lists in the site. PnP (Patterns and Practices) offers ListPicker control which allows selecting one or multiple available lists/libraries from the current SharePoint site.
 
In this article, we will explore the ListPicker control from PnP on how to use and configure it in the SPFx web part. We will develop a practical scenario to integrate ListPicker control in SPFx web part. 
 

Develop SharePoint Framework Web Part

 
Open a command prompt. Create a directory for the SPFx solution.
  1. md spfx-pnp-listpicker  
Navigate to the above created directory.
  1. cd spfx-pnp-listpicker  
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 (spfx-pnp-listpicker in this case) or type in any other name for your solution.
Selected choice: Hit enter
 
Target for the component: Here we can select the target environment where we are planning to deploy the client web part i.e. SharePoint Online or SharePoint On-Premises (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
 
Place of files: We may choose to use the same folder or create a sub-folder for our solution.
Selected choice: Same folder
 
Deployment option: Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
 
Permissions to access web APIs: Choose if the components in the solution require permission 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 a client side web part or an extension. Choose web part option.
Selected choice: WebPart
 
Web part name: Hit enter to select the default name or type in any other name.
Selected choice: PnPListPicker
 
Web part description: Hit enter to select the default description or type in any other value.
Selected choice: Use PnP LiskPicker control in SPFx solution
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice: React
 
Yeoman generator will perform the 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 the 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 .  

NPM Packages Dependency

 
@pnp/spfx-controls-react (https://sharepoint.github.io/sp-dev-fx-controls-react/)
 
SharePoint Framework React controls is an open source library that offers reusable set of React controls to be used in SPFx solution.
 
On the command prompt, run below command to include the npm package.
  1. npm install @pnp/spfx-controls-react --save   

Pass the context from web part to React Component

 
We will have to pass the SharePoint context from our web part to the React component to be used in ListPicker control.
 
Open React component properties at “src\webparts\pnPListPicker\components\IPnPListPickerProps.ts”
 
Add the below properties. 
  1. import { WebPartContext } from '@microsoft/sp-webpart-base';  
  2.   
  3. export interface IPnPListPickerProps {  
  4.   description: string;  
  5.   context: WebPartContext;  
  6. }  
From our main web part class (src\webparts\pnPWebPartTitle\PnPWebPartTitleWebPart.ts) pass the context to React component.
  1. export default class PnPListPickerWebPart extends BaseClientSideWebPart<IPnPListPickerWebPartProps> {  
  2.   
  3.   public render(): void {  
  4.     const element: React.ReactElement<IPnPListPickerProps > = React.createElement(  
  5.       PnPListPicker,  
  6.       {  
  7.         description: this.properties.description,  
  8.         context: this.context  
  9.       }  
  10.     );  
  11.   
  12.     ReactDom.render(element, this.domElement);  
  13.   }  
  14.      .  
  15.      .  
  16.      .  
  17. }  

Use ListPicker Control in the SPFx Web Part

 
During the implementation, we will use ListPicker control to allow end users to select one or multiple available lists/libraries of the current site.
 
Open the React component file at “src\webparts\pnPListPicker\components\PnPListPicker.tsx”
 
Add the below imports. 
  1. import { ListPicker } from "@pnp/spfx-controls-react/lib/ListPicker";  
Use the ListPicker control in the render method as follows.
  1. public render(): React.ReactElement<IPnPListPickerProps> {  
  2.   return (  
  3.     <div className={styles.pnPListPicker}>  
  4.       <div className={styles.container}>  
  5.         <div className={styles.row}>  
  6.           <div className={styles.column}>  
  7.             <span className={styles.title}>Welcome to SharePoint!</span>  
  8.             <p className={styles.subTitle}>PnP List Picker</p>  
  9.             <p className={styles.description}>PnP List Picker</p>  
  10.   
  11.             <ListPicker context={this.props.context}  
  12.               label="Select your list(s)"  
  13.               placeHolder="Select your list(s)"  
  14.               baseTemplate={100}  
  15.               includeHidden={false}  
  16.               multiSelect={false}  
  17.               onSelectionChanged={this.onListPickerChange} />  
  18.   
  19.           </div>  
  20.         </div>  
  21.       </div>  
  22.     </div>  
  23.   );  
  24. }  
Let us understand the ListPicker properties,
 
context The context of SPFx web part
baseTemplate SharePoint BaseTemplate ID to filter the list options
includeHidden Whether or not to include hidden lists
multiselect Specify if multi-choice selection is allowed
 
Implement the onSelectionChanged event which returns the list(s)
  1. private onListPickerChange (lists: string | string[]) {  
  2.     console.log("Lists: ", lists);  
  3. }  

Test the PnP WebPartTitle Control

  1. On the command prompt, type “gulp serve -nobrowser”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx
  4. Locate and add the web part (named PnPListPicker) to page.
 
The web part should display the SharePoint lists in a dropdown. Upon selection of the list from the dropdown, the list id will be logged to the browser console.
 

Summary

 
In this article, we explored the practical use of ListPicker control in the SPFx web part. The ListPicker control allows selecting one or multiple available lists/libraries from the current SharePoint site.
 
References