Tree view navigation using PnP Treeview control in the SharePoint Framework (SPFx) web part

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) {
    const sp = spfi().using(SPFx(this.props.context));
    this.state = {
      TreeLinks: []

  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: []
      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) {

    this.setState({ TreeLinks: treearr });

  public render(): React.ReactElement<ISpfxPnpTreeviewProps> {
    return (
      <div className={styles.spfxPnpTreeview}>
          onRenderItem={this.renderCustomTreeItem} />
  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 (
        <a href={} target={'_blank'}>

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!!!

8 thoughts on “Tree view navigation using PnP Treeview control in the SharePoint Framework (SPFx) web part

  1. Hi,
    After adding this webpart, the navigation elements supposed to display on the left side of the page(Left side site navigation bar) or it just displays on the page as a normal webpart?
    Because in the picture above it’s showing on the left side of the page just as the managed navigation or structural navigation. I was also expecting it to be on the left side but I got it in the center of the page itself.


  2. HI,
    I tried and added the webpart and it somehow worked. But here in this image( there’s a quick launch with links for Home, Notebook, Documents, etc,. I wonder if it’s possible to add this tree view navigation webpart there in the left quick launch pane? I can’t add this webpart there in the quick launch. Am I missing anything here?


  3. Hi Ravi,
    I was able to follow your example and managed to implement the treeview to load the terms under a termset from my managed metadata service. It works on both my SP2016 on prem and on my spo tenant. But the icons to expand and collapse a treeitem does not display on Prem but work fine in SPO.. any ideas on that?

    Liked by 1 person


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s