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 List View control in SPFx webpart.

PnP List Item View control

This control renders a list view for the given set of items. Refer to this link for more details and to see all the properties available for this control.
PnP List View Control In SharePoint Framework
PnP List View Control In SharePoint Framework
Note
I have created a custom list named Project and added the items as shown below.
PnP List View Control In SharePoint Framework
In this article, you will see how to perform the following tasks,
Prerequisites

Create SPFx solution

Open Node.js command prompt.
Create a new folder.
>md spfx-pnpreact-listview
Navigate to the folder.
> cd spfx-pnpreact-listview
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 List View Control In SharePoint Framework
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 List View solution

Execute the following command to install the PnP React Controls NPM package.
>npm install @pnp/spfx-controls-react –save
Execute the following command to install @pnp/sp package
>npm install @pnp/sp –save
Folder Structure
PnP List View Control In SharePoint Framework
Create a new folder named “models” inside webparts\pnPListView folder. Create a new file named as IListItem.ts. Open “src\webparts\pnPListView\models\IListItem.ts” file and update the code as shown below.
  1. export interface IListItem {
  2. Title: string;
  3. StartDate: string;
  4. EndDate: string;
  5. Status: string;
  6. }
  7. export interface IListItemColl {
  8. value: IListItem[];
  9. }
Create a new file named index.ts under models folder. Open “src\webparts\pnPListView\services\index.ts” file and update the code as shown below.
  1. export * from './IListItem';
Create a new file named as IPnPListViewState.ts under components folder. Open “src\webparts\pnPListView\components\IPnPListViewState.ts” file and update the code as shown below.
  1. import {IListItem, IListItemColl} from '../models/IListItem';
  2. export interface IPnPListViewState{
  3. items?: IListItem[];
  4. }
Create a new folder named “services” inside webparts\pnPListView folder. Create a new file named as ListViewService.ts. Open “src\webparts\pnPListView\services\ListViewService.ts” file and update the code as shown below. Import all the required modules and create getAllItems method which will retrieve data from SharePoint list using PnP.
  1. import { WebPartContext } from '@microsoft/sp-webpart-base';
  2. import { sp } from "@pnp/sp";
  3. import "@pnp/sp/webs";
  4. import "@pnp/sp/lists";
  5. import "@pnp/sp/items";
  6. import { IListItem, IListItemColl } from '../models/IListItem';
  7. export class ListViewService {
  8. public setup(context: WebPartContext): void {
  9. sp.setup({
  10. spfxContext: context
  11. });
  12. }
  13. public async getAllItems(listname: string): Promise<IListItem[]> {
  14. return new Promise<IListItem[]>(async (resolve, reject) => {
  15. try {
  16. var listItems: Array<IListItem> = new Array<IListItem>();
  17. sp.web.lists.getByTitle(listname).items.getAll().then((items) => {
  18. items.map((item) => {
  19. listItems.push({
  20. Title: item.Title,
  21. StartDate: item.StartDate,
  22. EndDate: item.EndDate,
  23. Status: item.Status
  24. });
  25. });
  26. resolve(listItems);
  27. });
  28. }
  29. catch (error) {
  30. console.log(error);
  31. }
  32. });
  33. }
  34. }
  35. const SPListViewService = new ListViewService();
  36. export default SPListViewService;
Create a new file named as index.ts under services folder. Open “src\webparts\pnPListView\services\index.ts” file and update the code as shown below.
  1. export * from './ListViewService';
Open “src\webparts\pnPListView\PnPListViewWebPart.ts” file and update the following.
Import modules
  1. import ListViewService from '../pnPListView/services/ListViewService';
Update the OnInit method
  1. protected onInit(): Promise<void> {
  2. return super.onInit().then(() => {
  3. ListViewService.setup(this.context);
  4. });
  5. }
Open “src\webparts\pnPListView\components\PnPListView.tsx” file and import the modules.
  1. import { IPnPListViewState } from './IPnPListViewState';
  2. import { ListView, IViewField, SelectionMode, GroupOrder, IGrouping } from "@pnp/spfx-controls-react/lib/ListView";
  3. import { IListItem, IListItemColl } from '../models/IListItem';
  4. import ListViewService from '../services/ListViewService';
Update the render method as shown below.
  1. public render(): React.ReactElement<IPnPListViewProps> {
  2. const { items = [] } = this.state;
  3. return (
  4. <div className={styles.pnPListView}>
  5. <ListView
  6. items={this.state.items}
  7. viewFields={viewFields}
  8. groupByFields={groupByFields}
  9. compact={true}
  10. selectionMode={SelectionMode.none}
  11. showFilter={true}
  12. filterPlaceHolder="Search..."
  13. />
  14. </div>
  15. );
Call the ListViewService to retrieve SharePoint list items.
  1. public _getItems = (): void => {
  2. ListViewService.getAllItems('Project').then(listItems => {
  3. console.log(listItems);
  4. this._items = listItems;
  5. this.setState({
  6. items: listItems
  7. });
  8. });
  9. }
Updated React component (src\webparts\pnPListView\components\PnPListView.tsx),
  1. import * as React from 'react';
  2. import styles from './PnPListView.module.scss';
  3. import { IPnPListViewProps } from './IPnPListViewProps';
  4. import { escape } from '@microsoft/sp-lodash-subset';
  5. import { IPnPListViewState } from './IPnPListViewState';
  6. import { ListView, IViewField, SelectionMode, GroupOrder, IGrouping } from "@pnp/spfx-controls-react/lib/ListView";
  7. import { IListItem, IListItemColl } from '../models/IListItem';
  8. import ListViewService from '../services/ListViewService';
  9. const viewFields: IViewField[] = [{
  10. name: "Title",
  11. displayName: "Title",
  12. isResizable: true,
  13. sorting: true,
  14. minWidth: 0,
  15. maxWidth: 150
  16. },
  17. {
  18. name: "StartDate",
  19. displayName: "StartDate",
  20. isResizable: true,
  21. sorting: true,
  22. minWidth: 0,
  23. maxWidth: 200
  24. },
  25. {
  26. name: "EndDate",
  27. displayName: "EndDate",
  28. isResizable: true,
  29. sorting: true,
  30. minWidth: 0,
  31. maxWidth: 200
  32. },
  33. {
  34. name: "Status",
  35. displayName: "Status",
  36. isResizable: true,
  37. sorting: true,
  38. minWidth: 0,
  39. maxWidth: 150
  40. },];
  41. const groupByFields: IGrouping[] = [
  42. {
  43. name: "Status",
  44. order: GroupOrder.ascending
  45. },];
  46. export default class PnPListView extends React.Component<IPnPListViewProps, IPnPListViewState> {
  47. private _items: IListItem[] = [];
  48. constructor(props: IPnPListViewProps, state: IPnPListViewState) {
  49. super(props);
  50. this.state = {
  51. items: []
  52. };
  53. }
  54. public componentDidMount(): void {
  55. this._getItems();
  56. }
  57. public render(): React.ReactElement<IPnPListViewProps> {
  58. const { items = [] } = this.state;
  59. return (
  60. <div className={styles.pnPListView}>
  61. <ListView
  62. items={this.state.items}
  63. viewFields={viewFields}
  64. groupByFields={groupByFields}
  65. compact={true}
  66. selectionMode={SelectionMode.none}
  67. showFilter={true}
  68. filterPlaceHolder="Search..."
  69. />
  70. </div>
  71. );
  72. }
  73. public _getItems = (): void => {
  74. ListViewService.getAllItems('Project').then(listItems => {
  75. console.log(listItems);
  76. this._items = listItems;
  77. this.setState({
  78. items: listItems
  79. });
  80. });
  81. }
  82. }

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-listview.sppkg). Click Deploy.
PnP List View Control In SharePoint Framework

Test the webpart

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

Summary

Thus, in this article, you saw how to use PnP List View control in SharePoint Framework.