SharePoint Framework web part usually offers configurable web part properties to configure itself. The values for web part properties can be set up later by the end-user to configure it as per the need. If the needed properties are not set on the web part, the same should be notified to the end-user in a graceful way. PnP (Patterns and Practices) offers Placeholder control to show the message for pending configurations.
During this article, we will explore the Placeholder control from PnP on how to use and configure it in SPFx web part. We will develop a practical scenario to integrate Placeholder control in SPFx web part.
Develop SharePoint Framework Web Part
Open a command prompt. Create a directory for SPFx solution.
Navigate to the above-created directory.
Run the Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
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 be 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 permissions 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: PnPPlaceholder
Web part description: Hit enter to select the default description or type in any other value.
Selected Choice: Use PnP Placeholder 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 a 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 project dependencies by running the below command.
In the command prompt type below command to open the solution in the code editor of your choice.
NPM Packages Dependency
@pnp/spfx-controls-react (https://sharepoint.github.io/sp-dev-fx-controls-react/)
SharePoint framework React control is an open-source library which offers a reusable set of React controls to be used in SPFx solution.
On the command prompt, run below command to include the npm package.
- 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 Placeholder control.
Open React component properties at “src\webparts\pnPPlaceholder\components\IPnPPlaceholderProps.ts”
Add below properties.
- import { WebPartContext } from '@microsoft/sp-webpart-base';
- import { DisplayMode } from '@microsoft/sp-core-library';
-
- export interface IPnPPlaceholderProps {
- description: string;
- displayMode: DisplayMode;
- context: WebPartContext;
- }
From our web part (src\webparts\pnPPlaceholder\PnPPlaceholderWebPart.ts) pass the context to React component,
- export default class PnPPlaceholderWebPart extends BaseClientSideWebPart<IPnPPlaceholderWebPartProps> {
-
- public render(): void {
- const element: React.ReactElement<IPnPPlaceholderProps> = React.createElement(
- PnPPlaceholder,
- {
- description: this.properties.description,
- displayMode: this.displayMode,
- context: this.context
- }
- );
-
- ReactDom.render(element, this.domElement);
- }
- .
- .
- .
- }
Use Placeholder Control in the SPFx Web Part
During the implementation, we will use Placeholder control to notify the user for missing configuration, if the out of box description property is not set. We will show the description when set instead of Placeholder control.
On the command prompt, type below command to open the solution in the code editor of your choice.
Open the React component file at “src\webparts\pnPPlaceholder\components\PnPPlaceholder.tsx”
Add below imports.
- import { Placeholder } from "@pnp/spfx-controls-react/lib/Placeholder";
- import { DisplayMode } from '@microsoft/sp-core-library';
Use the Placeholder control in the render method as follows.
- export default class PnPPlaceholder extends React.Component<IPnPPlaceholderProps, {}> {
- public render(): React.ReactElement<IPnPPlaceholderProps> {
- return (
- <div className={styles.pnPPlaceholder}>
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>Customize SharePoint experiences using Web Parts.</p>
-
- {}
- {this.props.description === "" &&
- <Placeholder iconName='Edit'
- iconText='Configure your web part'
- description='Please configure the web part.'
- buttonLabel='Configure'
- hideButton={this.props.displayMode === DisplayMode.Read}
- onConfigure={this._onConfigure} />
- }
-
- {}
- {this.props.description &&
- <p className={styles.description}>{escape(this.props.description)}</p>
- }
- </div>
- </div>
- </div>
- </div>
- );
- }
-
- private _onConfigure() {
-
- this.props.context.propertyPane.open();
- }
- }
The onConfigure property defines the action to be executed on the button click event (e.g. opening the property pane).
Test the PnP Placeholder Control
- On the command prompt, type “gulp serve”.
- Open SharePoint site.
- Navigate to /_layouts/15/workbench.aspx
- Locate and add the webpart (named PnPPlaceHolder) to page.
The web part by default is displayed as below (when the description web part property is set).
When the description web part property is not set, it will prompt the user to configure the web part.
Summary
In this article, we explored the practical use of Placeholder control in SPFx web part. The Placeholder control helps to show a message to the end-user if the web part configuration is pending.