PnP Folder Explorer Control 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 Folder Explorer control in SPFx webpart.
 

PnP Folder Explorer control

 
This control allows us to explore a folder structure by clicking on a folder to load its sub-folders and using a breadcrumb navigation to navigate back to a previous level. It also allows the user to create a new folder at the current level being explored. Refer to this link for more details.
 
Key Features
  • Ability to search
  • Add new folders
  • Load the subfolders
  • Breadcrumb navigation
PnP Folder Explorer Control In SharePoint Framework 
 
Note
I have added folders to Shared Documents library which are displayed in the Folder Explorer control. In the web part property pane, we can change the document library name and server relative URL from where the folders need to be retrieved.
 
PnP Folder Explorer Control In SharePoint Framework 
 
In this article, you will see how to perform the following tasks,
  • Prerequisites
  • Create SPFx solution
  • Implement Folder Explorer solution
  • Deploy the solution
  • Test the webpart
Prerequisites

Create SPFx solution

 
Open Node.js command prompt.
 
Create a new folder.
 
>md spfx-pnpreactcontrols
 
Navigate to the folder.
 
> cd spfx-pnpreactcontrols
 
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 Folder Explorer Control 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 Folder Explorer solution

 
Execute the following command to install the PnP React Controls NPM package.
 
>npm install @pnp/spfx-controls-react --save
 
Open “src\webparts\pnPFolderExplorer\components\IPnPFolderExplorerProps.ts” file and update the code as shown below.
 
Note
Folder name and server relative URL is configurable in the webpart property pane that’s why we are adding name and serverRelativeURL in props.
  1. import {WebPartContext} from '@microsoft/sp-webpart-base';  
  2.   
  3. export interface IPnPFolderExplorerProps {  
  4.   name: string;  
  5.   serverRelativeURL: string;  
  6.   context: WebPartContext;  
  7. }  
Open “src\webparts\pnPFolderExplorer\loc\mystrings.d.ts” file and update the code as shown below.
  1. declare interface IPnPFolderExplorerWebPartStrings {  
  2.   PropertyPaneDescription: string;  
  3.   BasicGroupName: string;  
  4.   NameFieldLabel: string;  
  5.   ServerRelativeURLFieldLabel: string;  
  6. }  
  7.   
  8. declare module 'PnPFolderExplorerWebPartStrings' {  
  9.   const strings: IPnPFolderExplorerWebPartStrings;  
  10.   export = strings;  
  11. }  
Open “src\webparts\pnPFolderExplorer\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.     "NameFieldLabel""Folder Name",  
  6.     "ServerRelativeURLFieldLabel""Server Relative URL"  
  7.   }  
  8. });  
Open “src\webparts\pnPFolderExplorer\PnPFolderExplorerWebPart.ts” file and update the following.
  • Update the interface
  • Update the render method
  • Update the getPropertyPaneConfiguration method
Update the interface:
  1. export interface IPnPFolderExplorerWebPartProps {  
  2.   name: string;  
  3.   serverRelativeURL: string;  
  4. }  
Update the render method:
  1. public render(): void {  
  2.    const element: React.ReactElement<IPnPFolderExplorerProps> = React.createElement(  
  3.      PnPFolderExplorer,  
  4.      {  
  5.        name: this.properties.name,  
  6.        serverRelativeURL: this.properties.serverRelativeURL,  
  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('name', {  
  13.                   label: strings.NameFieldLabel  
  14.                 }),  
  15.                 PropertyPaneTextField('serverRelativeURL', {  
  16.                   label: strings.ServerRelativeURLFieldLabel  
  17.                 })  
  18.               ]  
  19.             }  
  20.           ]  
  21.         }  
  22.       ]  
  23.     };  
  24.   }  
Open “src\webparts\pnPFolderExplorer\components\PnPFolderExplorer.tsx” file and import the control.
  1. import { FolderExplorer, IFolder } from "@pnp/spfx-controls-react/lib/FolderExplorer";  
Update the render method as shown below.
  1. public render(): React.ReactElement<IPnPFolderExplorerProps> {  
  2.     return (  
  3.       <div className={styles.pnPFolderExplorer}>  
  4.         <FolderExplorer context={this.props.context}  
  5.           rootFolder={{  
  6.             Name: this.props.name,  
  7.             ServerRelativeUrl: this.props.serverRelativeURL  
  8.           }}  
  9.           defaultFolder={{  
  10.             Name: this.props.name,  
  11.             ServerRelativeUrl: this.props.serverRelativeURL  
  12.           }}  
  13.           onSelect={this._onFolderSelect}  
  14.           canCreateFolders={true} />  
  15.       </div>  
  16.     );  
  17.   }  
onSelect change event returns the selected folder:
  1. private _onFolderSelect = (folder: IFolder): void => {  
  2.    console.log('selected folder', folder);  
  3.  }  
Updated React component (src\webparts\pnPFolderExplorer\components\PnPFolderExplorer.tsx):
  1. import * as React from 'react';  
  2. import styles from './PnPFolderExplorer.module.scss';  
  3. import { IPnPFolderExplorerProps } from './IPnPFolderExplorerProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import { FolderExplorer, IFolder } from "@pnp/spfx-controls-react/lib/FolderExplorer";  
  6.   
  7. export default class PnPFolderExplorer extends React.Component<IPnPFolderExplorerProps, {}> {  
  8.   public render(): React.ReactElement<IPnPFolderExplorerProps> {  
  9.     return (  
  10.       <div className={styles.pnPFolderExplorer}>  
  11.         <FolderExplorer context={this.props.context}  
  12.           rootFolder={{  
  13.             Name: this.props.name,  
  14.             ServerRelativeUrl: this.props.serverRelativeURL  
  15.           }}  
  16.           defaultFolder={{  
  17.             Name: this.props.name,  
  18.             ServerRelativeUrl: this.props.serverRelativeURL  
  19.           }}  
  20.           onSelect={this._onFolderSelect}  
  21.           canCreateFolders={true} />  
  22.       </div>  
  23.     );  
  24.   }  
  25.   private _onFolderSelect = (folder: IFolder): void => {  
  26.     console.log('selected folder', folder);  
  27.   }  
  28. }  

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-pnpreactcontrols.sppkg). Click Deploy.
 
PnP Folder Explorer Control In SharePoint Framework 
 

Test the webpart

 
Navigate to the SharePoint site and add the app.
 
PnP Folder Explorer Control In SharePoint Framework 
 
Navigate to the page and add the webpart as shown below.
 
PnP Folder Explorer Control In SharePoint Framework 
 
Edit the webpart and update the folder name and server relative URL.
 
PnP Folder Explorer Control In SharePoint Framework
 
PnP Folder Explorer Control In SharePoint Framework 
 

Summary

 
Thus, in this article, you saw how to use PnP Folder Explorer control in SharePoint Framework.