PnP Site Breadcrumb 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 Site Breadcrumb control in SPFx webpart.
 

PnP Site Breadcrumb Control

 
This control returns a breadcrumb based on the current location. Refer to this link for more details.
 
PnP Site Breadcrumb Control In SharePoint Framework 
 
In this article, you will see how to perform the following tasks,
  • Prerequisites
  • Create SPFx solution
  • Implement Site Breadcrumb Control solution
  • Deploy the solution
  • Test the webpart
Prerquisites

Create SPFx solution

 
Open Node.js command prompt.
 
Create a new folder.
 
>md spfx-pnpreact-SiteBreadcrumb
 
Navigate to the folder.
 
> cd spfx-pnpreact-SiteBreadcrumb
 
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 Site Breadcrumb 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 Site Breadcrumb Control solution

 
Execute the following command to install the PnP React Controls NPM package.
 
>npm install @pnp/spfx-controls-react --save
 
Open “src\webparts\pnPSiteBreadcrumb\components\IPnPSiteBreadcrumbProps.ts” file and update the code as shown below.
  1. import { WebPartContext } from '@microsoft/sp-webpart-base';  
  2.   
  3. export interface IPnPSiteBreadcrumbProps {  
  4.   description: string;  
  5.   context: WebPartContext;  
  6. }  
Open webpart file “src\webparts\pnPSiteBreadcrumb\PnPSiteBreadcrumbWebPart.ts” and update the following.
  • Update the render method
Update the render method,
  1. public render(): void {  
  2.   const element: React.ReactElement<IPnPSiteBreadcrumbProps> = React.createElement(  
  3.     PnPSiteBreadcrumb,  
  4.     {  
  5.       description: this.properties.description,  
  6.       context: this.context  
  7.     }  
  8.   );  
  9.   
  10.   ReactDom.render(element, this.domElement);  
  11. }  
Open “src\webparts\pnPSiteBreadcrumb\components\PnPSiteBreadcrumb.tsx” file and import the control.
  1. import { SiteBreadcrumb } from "@pnp/spfx-controls-react/lib/SiteBreadcrumb";  
Update the render method as shown below.
  1. public render(): React.ReactElement<IPnPSiteBreadcrumbProps> {  
  2.   return (  
  3.     <div className={ styles.pnPSiteBreadcrumb }>  
  4.       <SiteBreadcrumb context={this.props.context} />  
  5.       <div className={ styles.container }>  
  6.         <div className={ styles.row }>  
  7.           <div className={ styles.column }>  
  8.             <span className={ styles.title }>Welcome to SharePoint!</span>  
  9.             <p className={ styles.subTitle }>Customize SharePoint experiences using Web Parts.</p>  
  10.             <p className={ styles.description }>{escape(this.props.description)}</p>  
  11.             <a href="https://aka.ms/spfx" className={ styles.button }>  
  12.               <span className={ styles.label }>Learn more</span>  
  13.             </a>  
  14.           </div>  
  15.         </div>  
  16.       </div>  
  17.     </div>  
  18.   );  
  19. }  
Updated React component (src\webparts\pnPSiteBreadcrumb\components\PnPSiteBreadcrumb.tsx),
  1. import * as React from 'react';  
  2. import styles from './PnPSiteBreadcrumb.module.scss';  
  3. import { IPnPSiteBreadcrumbProps } from './IPnPSiteBreadcrumbProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import { SiteBreadcrumb } from "@pnp/spfx-controls-react/lib/SiteBreadcrumb";  
  6.   
  7. export default class PnPSiteBreadcrumb extends React.Component<IPnPSiteBreadcrumbProps, {}> {  
  8.   public render(): React.ReactElement<IPnPSiteBreadcrumbProps> {  
  9.     return (  
  10.       <div className={styles.pnPSiteBreadcrumb}>  
  11.         <SiteBreadcrumb context={this.props.context} />  
  12.         <div className={styles.container}>  
  13.           <div className={styles.row}>  
  14.             <div className={styles.column}>  
  15.               <span className={styles.title}>Welcome to SharePoint!</span>  
  16.               <p className={styles.subTitle}>Customize SharePoint experiences using Web Parts.</p>  
  17.               <p className={styles.description}>{escape(this.props.description)}</p>  
  18.               <a href="https://aka.ms/spfx" className={styles.button}>  
  19.                 <span className={styles.label}>Learn more</span>  
  20.               </a>  
  21.             </div>  
  22.           </div>  
  23.         </div>  
  24.       </div>  
  25.     );  
  26.   }  
  27. }  

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-site-breadcrumb.sppkg). Click Deploy.
 

Test the webpart

 
Navigate to the SharePoint site and add the app.
 
PnP Site Breadcrumb Control In SharePoint Framework 
 
Navigate to the page and add the webpart as shown below.
 
PnP Site Breadcrumb Control In SharePoint Framework 
 
Result
 
PnP Site Breadcrumb Control In SharePoint Framework 
 

Summary

 
Thus, in this article, you saw how to use PnP Site Breadcrumb Control in SharePoint Framework.