This article provides steps to implement the Tree view navigation using PnP Treeview control in the SharePoint Framework (SPFx), generally, Treeview control allows to present a hierarchical view of information. Each tree item can have a number of subitems. This is often visualized by an indentation in a list. A tree item can be expanded to reveal subitems (if exist), and collapsed to hide subitems. in this article, we building the dynamic treeview navigation and data stored in the SharePoint list

Create a new web part project
Open power shell and run the following comment to create a new web part by running the Yeoman SharePoint Generator
yo @microsoft/sharepoint
Install the required packages
npm install @pnp/sp --save
npm install @pnp/spfx-controls-react --save --save-exact
React Component
import * as React from 'react';
import styles from './SpfxPnpTreeview.module.scss';
import { ISpfxPnpTreeviewProps, ISpfxPnpTreeviewState } from './ISpfxPnpTreeview';
import { TreeView, ITreeItem, TreeViewSelectionMode, TreeItemActionsDisplayMode } from "@pnp/spfx-controls-react/lib/TreeView";
import { SPFI, spfi,SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
export default class SpfxPnpTreeview extends React.Component<ISpfxPnpTreeviewProps, ISpfxPnpTreeviewState> {
constructor(props: ISpfxPnpTreeviewProps) {
super(props);
const sp = spfi().using(SPFx(this.props.context));
this.state = {
TreeLinks: []
}
this._getLinks(sp);
}
private async _getLinks(sp) {
const allItems: any[] = await sp.web.lists.getByTitle("TreeLinks").items();
var treearr: ITreeItem[] = [];
allItems.forEach(function (v, i) {
if (v["ParentId"] == null) {
const tree: ITreeItem = {
key: v.Id,
label: v["Title"],
data: v["Link"],
children: []
}
treearr.push(tree);
}
else {
const tree: ITreeItem = {
key: v.Id,
label: v["Title"],
data: v["Link"]
}
var treecol: Array<ITreeItem> = treearr.filter(function (value) { return value.key == v["ParentId"] })
if (treecol.length != 0) {
treecol[0].children.push(tree);
}
}
console.log(v);
});
console.log(treearr);
this.setState({ TreeLinks: treearr });
}
public render(): React.ReactElement<ISpfxPnpTreeviewProps> {
return (
<div className={styles.spfxPnpTreeview}>
<TreeView
items={this.state.TreeLinks}
defaultExpanded={false}
selectionMode={TreeViewSelectionMode.None}
selectChildrenIfParentSelected={true}
showCheckboxes={true}
treeItemActionsDisplayMode={TreeItemActionsDisplayMode.Buttons}
onSelect={this.onTreeItemSelect}
onExpandCollapse={this.onTreeItemExpandCollapse}
onRenderItem={this.renderCustomTreeItem} />
</div>
);
}
private onTreeItemSelect(items: ITreeItem[]) {
console.log("Items selected: ", items);
}
private onTreeItemExpandCollapse(item: ITreeItem, isExpanded: boolean) {
console.log((isExpanded ? "Item expanded: " : "Item collapsed: ") + item);
}
private renderCustomTreeItem(item: ITreeItem): JSX.Element {
return (
<span>
<a href={item.data} target={'_blank'}>
{item.label}
</a>
</span>
);
}
}
Props/State Interface
import { ITreeItem } from "@pnp/spfx-controls-react/lib/TreeView";
export interface ISpfxPnpTreeviewState {
TreeLinks: ITreeItem[];
}
export interface ISpfxPnpTreeviewProps {
description: string;
context: any | null;
}
Deploy the solution
You’re now ready to build, bundle, package, and deploy the solution.
Run the gulp commands to verify that the solution builds correctly.
gulp build
Use the following command to bundle and package the solution.
gulp bundle --ship
gulp package-solution --ship
Browse to the app catalog of your target tenant and upload the solution package. You can find the solution package under the sharepoint/solution folder of your solution. It is the .sppkg file. After you upload the solution package in the app catalog. you can find and the web part anywhere across the tenant.
Sharing is caring!
If you have any questions, feel free to let me know in the comments section.
Happy coding!!!