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 Security Trimmed control in SPFx webpart.
PnP Security Trimmed Control
This control is intended to be used when you want to show or hide components based on the user permissions.
The control can be used to check the user’s permissions on the current site / list were the solution is loaded, or on a remote site / list. Refer to
this link for more details.
Security Trimmed Control can be used check the following permissions,
- Check permissions on the current site: level={PermissionLevel.currentWeb}
- Check permissions on the current list: level={PermissionLevel.currentList}
- Check permissions on remote site: level={PermissionLevel.remoteWeb}
- Check permissions on remote list/library: level={PermissionLevel.remoteListOrLib}
SPPermission – Refer to this
link to get the complete list of built-in permissions.
Example
User with manage web permission will see 2 components.
Users with view pages permission will see one component.
In this article, you will see how to perform the following tasks,
- Prerequisites
- Create SPFx solution
- Implement Security Trimmed Control solution
- Deploy the solution
- Test the webpart
Prerequisites
Create SPFx solution
Open Node.js command prompt.
Create a new folder.
>md spfx-pnpreact-SecurityTrimmedControl
Navigate to the folder.
> cd spfx-pnpreact-SecurityTrimmedControl
Execute the following command to create SPFx webpart.
>yo @microsoft/sharepoint
Enter all the required details to create a new solution as shown below.
Yeoman generator will perform the 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 Security Trimmed Control solution
Execute the following command to install the PnP React Controls NPM package.
>npm install @pnp/spfx-controls-react --save
Open “src\webparts\pnPSecurityTrimmedControl\components\IPnPSecurityTrimmedControlProps.ts” file and update the code as shown below.
- import {WebPartContext} from '@microsoft/sp-webpart-base';
-
- export interface IPnPSecurityTrimmedControlProps {
- description: string;
- context: WebPartContext;
- }
Open webpart file “src\webparts\pnPSecurityTrimmedControl\PnPSecurityTrimmedControlWebPart.ts” and update the following.
Update the render method,
- public render(): void {
- const element: React.ReactElement<IPnPSecurityTrimmedControlProps> = React.createElement(
- PnPSecurityTrimmedControl,
- {
- description: this.properties.description,
- context: this.context
- }
- );
-
- ReactDom.render(element, this.domElement);
- }
Open “src\webparts\pnPSecurityTrimmedControl\components\PnPSecurityTrimmedControl.tsx” file and import the control.
- import { SecurityTrimmedControl, PermissionLevel } from "@pnp/spfx-controls-react/lib/SecurityTrimmedControl";
- import { SPPermission } from '@microsoft/sp-page-context';
Update the render method as shown below.
- public render(): React.ReactElement<IPnPSecurityTrimmedControlProps> {
- return (
- <div className={styles.pnPSecurityTrimmedControl}>
- <SecurityTrimmedControl context={this.props.context}
- level={PermissionLevel.currentWeb}
- permissions={[SPPermission.manageWeb]}>
- {
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>This webpart is for Administrators.</p>
- </div>
- </div>
- </div>
- }
- </SecurityTrimmedControl>
- <SecurityTrimmedControl context={this.props.context}
- level={PermissionLevel.currentWeb}
- permissions={[SPPermission.viewPages]}>
- {
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>This webpart is for Users.</p>
- </div>
- </div>
- </div>
- }
- </SecurityTrimmedControl>
- </div>
- );
- }
- }
Updated React component (src\webparts\pnPSecurityTrimmedControl\components\PnPSecurityTrimmedControl.tsx),
- import * as React from 'react';
- import styles from './PnPSecurityTrimmedControl.module.scss';
- import { IPnPSecurityTrimmedControlProps } from './IPnPSecurityTrimmedControlProps';
- import { escape } from '@microsoft/sp-lodash-subset';
- import { SecurityTrimmedControl, PermissionLevel } from "@pnp/spfx-controls-react/lib/SecurityTrimmedControl";
- import { SPPermission } from '@microsoft/sp-page-context';
-
- export default class PnPSecurityTrimmedControl extends React.Component<IPnPSecurityTrimmedControlProps, {}> {
- public render(): React.ReactElement<IPnPSecurityTrimmedControlProps> {
- return (
- <div className={styles.pnPSecurityTrimmedControl}>
- <SecurityTrimmedControl context={this.props.context}
- level={PermissionLevel.currentWeb}
- permissions={[SPPermission.manageWeb]}>
- {
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>This webpart is for Administrators.</p>
- </div>
- </div>
- </div>
- }
- </SecurityTrimmedControl>
- <SecurityTrimmedControl context={this.props.context}
- level={PermissionLevel.currentWeb}
- permissions={[SPPermission.viewPages]}>
- {
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>This webpart is for Users.</p>
- </div>
- </div>
- </div>
- }
- </SecurityTrimmedControl>
- </div>
- );
- }
- }
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-security-trimmed-control.sppkg). Click Deploy.
Test the webpart
Navigate to the SharePoint site and add the app.
Navigate to the page and add the webpart as shown below.
Result
User who has manage web permission will be able to see both the components in the webpart.
User who has view pages permission will be able to see only one component in the webpart.
Summary
Thus, in this article, you saw how to how to use PnP Security Trimmed Control in SharePoint Framework.