PnP Web Part Title 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 Web Part Title control in SPFx webpart.
 

PnP Web Part Title Control

 
This control renders a web part title that is changeable when the page is in edit mode. See that all links also can be added as shown below. Refer to  this link for more details.
 
PnP Web Part Title Control In SharePoint Framework 
 
In this article, you will see how to perform the following tasks,
  • Prerequisites
  • Create SPFx solution
  • Implement Web Part Title Control solution
  • Deploy the solution
  • Test the webpart
Prerequisites

Create SPFx solution

 
Open Node.js command prompt.
 
Create a new folder.
 
>md spfx-pnpreact-webparttitle
 
Navigate to the folder.
 
> cd spfx-pnpreact-webparttitle
 
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 Web Part Title 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 Web Part Title Control solution

 
Execute the following command to install the PnP React Controls NPM package.
 
>npm install @pnp/spfx-controls-react --save
 
Open “src\webparts\pnPWebPartTitle\components\IPnPWebPartTitleProps.ts” file and update the code as shown below.
  1. import { DisplayMode } from '@microsoft/sp-core-library';  
  2.   
  3. export interface IPnPWebPartTitleProps {  
  4.   description: string;  
  5.   title: string;  
  6.   displayMode: DisplayMode;  
  7.   updateProperty: (value: string) => void;  
  8. }  
Open webpart file “src\webparts\pnPWebPartTitle\PnPWebPartTitleWebPart.ts” and update the following.
  • Update the webpart props interface
  • Update the render method
Update the webpart props interface:
  1. export interface IPnPWebPartTitleWebPartProps {  
  2.   description: string;  
  3.   title: string;  
  4.   displayMode: DisplayMode;  
  5.   updateProperty: (value: string) => void;  
  6. }  
Update the webpart props interface,
  1. public render(): void {  
  2.   const element: React.ReactElement<IPnPWebPartTitleProps> = React.createElement(  
  3.     PnPWebPartTitle,  
  4.     {  
  5.       description: this.properties.description,  
  6.       title: this.properties.title,  
  7.       displayMode: this.displayMode,  
  8.       updateProperty: (value: string) => {  
  9.         this.properties.title = value;  
  10.       }  
  11.     }  
  12.   );  
  13.   
  14.   ReactDom.render(element, this.domElement);  
  15. }  
Open the component file “src\webparts\pnPWebPartTitle\components\PnPWebPartTitle.tsx” and import the control.
  1. import { WebPartTitle } from "@pnp/spfx-controls-react/lib/WebPartTitle";  
  2. import { Link } from 'office-ui-fabric-react';  
Update the render method as shown below.
  1. public render(): React.ReactElement<IPnPWebPartTitleProps> {  
  2.    return (  
  3.      <div className={styles.pnPWebPartTitle}>  
  4.        <WebPartTitle displayMode={this.props.displayMode}  
  5.          title={this.props.title}  
  6.          updateProperty={this.props.updateProperty}  
  7.          moreLink={  
  8.            <Link href="https://sharepoint.github.io/sp-dev-fx-controls-react/" >See all</Link>  
  9.          } />  
  10.        <div className={styles.container}>  
  11.          <div className={styles.row}>  
  12.            <div className={styles.column}>  
  13.              <span className={styles.title}>Welcome to SharePoint!</span>  
  14.              <p className={styles.subTitle}>Customize SharePoint experiences using Web Parts.</p>  
  15.              <p className={styles.description}>{escape(this.props.description)}</p>  
  16.              <a href="https://aka.ms/spfx" className={styles.button}>  
  17.                <span className={styles.label}>Learn more</span>  
  18.              </a>  
  19.            </div>  
  20.          </div>  
  21.        </div>  
  22.      </div>  
  23.    );  
  24.  }  
Updated React component (src\webparts\pnPWebPartTitle\components\PnPWebPartTitle.tsx),
  1. import * as React from 'react';  
  2. import styles from './PnPWebPartTitle.module.scss';  
  3. import { IPnPWebPartTitleProps } from './IPnPWebPartTitleProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5. import { WebPartTitle } from "@pnp/spfx-controls-react/lib/WebPartTitle";  
  6. import { Link } from 'office-ui-fabric-react';  
  7.   
  8. export default class PnPWebPartTitle extends React.Component<IPnPWebPartTitleProps, {}> {  
  9.   public render(): React.ReactElement<IPnPWebPartTitleProps> {  
  10.     return (  
  11.       <div className={styles.pnPWebPartTitle}>  
  12.         <WebPartTitle displayMode={this.props.displayMode}  
  13.           title={this.props.title}  
  14.           updateProperty={this.props.updateProperty}  
  15.           moreLink={  
  16.             <Link href="https://sharepoint.github.io/sp-dev-fx-controls-react/" >See all</Link>  
  17.           } />  
  18.         <div className={styles.container}>  
  19.           <div className={styles.row}>  
  20.             <div className={styles.column}>  
  21.               <span className={styles.title}>Welcome to SharePoint!</span>  
  22.               <p className={styles.subTitle}>Customize SharePoint experiences using Web Parts.</p>  
  23.               <p className={styles.description}>{escape(this.props.description)}</p>  
  24.               <a href="https://aka.ms/spfx" className={styles.button}>  
  25.                 <span className={styles.label}>Learn more</span>  
  26.               </a>  
  27.             </div>  
  28.           </div>  
  29.         </div>  
  30.       </div>  
  31.     );  
  32.   }  
  33. }  

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
 
Go to the Apps for SharePoint library and upload the package file (sharepoint\solution\spfx-pnpreact-webparttitle.sppkg). Click Deploy.
 

Test the webpart

 
Navigate to the SharePoint site and add the app.
 
PnP Web Part Title Control In SharePoint Framework 
 
Navigate to the page and add the webpart as shown below.
 
PnP Web Part Title Control In SharePoint Framework 
 
Result
 
Webpart added successfully.
 
PnP Web Part Title Control In SharePoint Framework 
 
Update the webpart title and publish the page. You can also see see all links in the webpart in the top right corner.
 
PnP Web Part Title Control In SharePoint Framework 
 
Edit the webpart to update the webpart title.
 

Summary

 
Thus, in this article, you saw how to use PnP Web Part Title Control in SharePoint Framework.