PnP List Picker And Fluent UI Dialog Control 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.
 

PnP List Picker control

 
This control allows you to select one or multiple available lists/libraries of the current site. Refer to this link for more details and to see all the properties available for this control.
 

Fluent UI

 
A collection of UX frameworks use to build Fluent experiences that fit seamlessly into a broad range of Microsoft products.
 

Fluent UI Dialog

 
Dialogs are temporary, modal UI overlays that generally provide contextual app information or require user confirmation/input. Refer to this link for more details.
 
List picker control displays all the custom lists from the current site and the user can select the list. The selected list can be deleted by clicking the button and a dialog pops up for confirmation. PnP JS is used to delete the list.
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 
In this article, you will see how to perform the following tasks,
  • Prerequisites
  • Create SPFx solution
  • Implement the solution
  • Deploy the solution
  • Test the webpart
Prerequisites

Create SPFx solution

 
Open the Node.js command prompt.
 
Create a new folder.
 
>md fluentui-dialogcontrol
 
Navigate to the folder.
 
>cd fluentui-dialogcontrol
 
Execute the following command to create a SPFx webpart.
 
>yo @microsoft/sharepoint
 
Enter all the required details to create a new solution, as shown below:
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 
Yeoman generator will perform the scaffolding process. 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 the solution

 
Execute the following command to install the PnP React Controls NPM package.
 
>npm install @pnp/spfx-controls-react –save
 
Execute the following command to install @pnp/sp package
 
>npm install @pnp/sp –save
 
Open props file “fluentui-dialogcontrol\src\webparts\dialogcontrol\components\IDialogcontrolProps.ts” and update the code as shown below.
  1. import {WebPartContext} from '@microsoft/sp-webpart-base';  
  2.   
  3. export interface IDialogcontrolProps {  
  4.   description: string;  
  5.   context: WebPartContext;  
  6. }  
Create a new file named as IDialogcontrolState.ts under the components folder. Open the“fluentui-dialogcontrol\src\webparts\dialogcontrol\components\IDialogcontrolState.ts” file and update the code, as shown below:
  1. export interface IDialogcontrolState{  
  2.     hideDialog: boolean;  
  3.     listName: string;  
  4. }  
Open the webpart file “fluentui-dialogcontrol\src\webparts\dialogcontrol\DialogcontrolWebPart.ts” and update the following.
  • Import modules
  • Update the OnInit method
Import modules:
  1. import { sp } from "@pnp/sp";  
Update the OnInit method:
  1. public onInit(): Promise<void> {  
  2.     return super.onInit().then(_ => {  
  3.       sp.setup({  
  4.         spfxContext: this.context  
  5.       });  
  6.     });  
  7.   }  
Update the render method:
  1. public render(): void {  
  2.     const element: React.ReactElement<IDialogcontrolProps> = React.createElement(  
  3.       Dialogcontrol,  
  4.       {  
  5.         description: this.properties.description,  
  6.         context: this.context  
  7.       }  
  8.     );  
Open the component file “fluentui-dialogcontrol\src\webparts\dialogcontrol\components\Dialogcontrol.tsx” and import the modules.
  1. import { IDialogcontrolState } from './IDialogcontrolState';  
  2. import { Dialog, DialogType, DialogFooter } from 'office-ui-fabric-react/lib/Dialog';  
  3. import { PrimaryButton, DefaultButton } from 'office-ui-fabric-react/lib/Button';  
  4. import { ListPicker } from "@pnp/spfx-controls-react/lib/ListPicker";  
  5. import { autobind } from 'office-ui-fabric-react';  
  6. import { sp } from "@pnp/sp";  
  7. import "@pnp/sp/webs";  
  8. import "@pnp/sp/lists";  
Update the render method, as shown below.
  1. public render(): React.ReactElement<IDialogcontrolProps> {  
  2.     return (  
  3.       <div>  
  4.         <ListPicker context={this.props.context}  
  5.           label="Select your list(s)"  
  6.           placeHolder="Select your list(s)"  
  7.           baseTemplate={100}  
  8.           includeHidden={false}  
  9.           multiSelect={false}  
  10.           onSelectionChanged={this.onListPickerChange} />  
  11.         <div>  
  12.           <DefaultButton secondaryText="Opens the Sample Dialog" onClick={this._showDialog} text="Delete List" />  
  13.         </div>  
  14.   
  15.         <Dialog  
  16.           hidden={this.state.hideDialog}  
  17.           onDismiss={this._closeDialog}  
  18.           dialogContentProps={{  
  19.             type: DialogType.close,  
  20.             title: 'Delete?',  
  21.             subText:  
  22.               'Are you sure you want to send the item(s) to the site Recycle Bin?',  
  23.           }}  
  24.   
  25.           modalProps={{  
  26.             isBlocking: false,  
  27.             styles: { main: { maxWidth: 450 } },  
  28.           }}  
  29.         >  
  30.           <DialogFooter>  
  31.             <PrimaryButton onClick={this._closeDialog} text="Delete" />  
  32.           </DialogFooter>  
  33.         </Dialog>  
  34.       </div>  
  35.     );  
  36.   }  
Add the helper methods:
  1. @autobind  
  2.   private onListPickerChange(list: string) {  
  3.     this.setState({ listName: list });  
  4.   }  
  5.   
  6.   private _showDialog = (): void => {  
  7.     this.setState({ hideDialog: false });  
  8.   }  
  9.   
  10.   private _closeDialog = (): void => {  
  11.     sp.web.lists.getById(this.state.listName).recycle();  
  12.     this.setState({ hideDialog: true });  
  13.     this.render();  
  14.   } 
Updated React component (fluentui-dialogcontrol\src\webparts\dialogcontrol\components\Dialogcontrol.tsx):
  1. import * as React from 'react';  
  2. import styles from './Dialogcontrol.module.scss';  
  3. import { IDialogcontrolProps } from './IDialogcontrolProps';  
  4. import { IDialogcontrolState } from './IDialogcontrolState';  
  5. import { escape } from '@microsoft/sp-lodash-subset';  
  6. import { Dialog, DialogType, DialogFooter } from 'office-ui-fabric-react/lib/Dialog';  
  7. import { PrimaryButton, DefaultButton } from 'office-ui-fabric-react/lib/Button';  
  8. import { ListPicker } from "@pnp/spfx-controls-react/lib/ListPicker";  
  9. import { autobind } from 'office-ui-fabric-react';  
  10. import { sp } from "@pnp/sp";  
  11. import "@pnp/sp/webs";  
  12. import "@pnp/sp/lists";  
  13.   
  14. export default class Dialogcontrol extends React.Component<IDialogcontrolProps, IDialogcontrolState> {  
  15.   constructor(props: IDialogcontrolProps) {  
  16.     super(props);  
  17.     this.state = {  
  18.       hideDialog: true,  
  19.       listName: ''  
  20.     };  
  21.   }  
  22.   
  23.   public render(): React.ReactElement<IDialogcontrolProps> {  
  24.     return (  
  25.       <div>  
  26.         <ListPicker context={this.props.context}  
  27.           label="Select your list(s)"  
  28.           placeHolder="Select your list(s)"  
  29.           baseTemplate={100}  
  30.           includeHidden={false}  
  31.           multiSelect={false}  
  32.           onSelectionChanged={this.onListPickerChange} />  
  33.         <div>  
  34.           <DefaultButton secondaryText="Opens the Sample Dialog" onClick={this._showDialog} text="Delete List" />  
  35.         </div>  
  36.   
  37.         <Dialog  
  38.           hidden={this.state.hideDialog}  
  39.           onDismiss={this._closeDialog}  
  40.           dialogContentProps={{  
  41.             type: DialogType.close,  
  42.             title: 'Delete?',  
  43.             subText:  
  44.               'Are you sure you want to send the item(s) to the site Recycle Bin?',  
  45.           }}  
  46.   
  47.           modalProps={{  
  48.             isBlocking: false,  
  49.             styles: { main: { maxWidth: 450 } },  
  50.           }}  
  51.         >  
  52.           <DialogFooter>  
  53.             <PrimaryButton onClick={this._closeDialog} text="Delete" />  
  54.           </DialogFooter>  
  55.         </Dialog>  
  56.       </div>  
  57.     );  
  58.   }  
  59.   
  60.   @autobind  
  61.   private onListPickerChange(list: string) {  
  62.     this.setState({ listName: list });  
  63.   }  
  64.   
  65.   private _showDialog = (): void => {  
  66.     this.setState({ hideDialog: false });  
  67.   }  
  68.   
  69.   private _closeDialog = (): void => {  
  70.     sp.web.lists.getById(this.state.listName).recycle();  
  71.     this.setState({ hideDialog: true });  
  72.     this.render();  
  73.   }  
  74. }  

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 (fluentui-dialogcontrol\sharepoint\solution\fluentui-dialogcontrol.sppkg). Click Deploy.
 

Test the webpart

 
Navigate to the SharePoint site and add the app.
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 
Navigate to the page and add the webpart. Select the list from the list picker control and click the delete list button. A confirmation dialog pops up and click on Delete. The List should delete successfully.
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 
PnP List Picker And Fluent UI Dialog Control In SharePoint Framework
 

Summary

 
Thus, in this article, you saw how to use PnP List Picker and Fluent UI Dialog control in SharePoint Framework.