SharePoint Framework is a new development model which eases the developer life for developing and testing the application in automated ways when compared to other development models and it also enriches the user’s UI experience in SharePoint sites.

Below are some references for developing the SharePoint framework applications,

Introduction to SharePoint Framework Development This link contains the summarization of the tools required for developing framework model web part, development perquisites and how to create a project

Tutorials and Walkthroughs This contains the below tutorial links to the starting point for SharePoint Framework development

In this article, we will create a HelloWorld Web Part to talk to SharePoint by using PnP JavaScript Library (PnP JS version of Tutorial 2: HelloWorld Web Part, Talking to SharePoint).

Before creating a SharePoint Framework solution, we have to ensure the below items for development,

  • The environment should be in Office 365 developer tenant, due to the current release is initial preview stage
  • SharePoint site should be a developer site collection
  • Node Js and npmjs tools.
  • Yeoman and Gulp should be installed globally.
  • Yeoman SharePoint Generator should be installed globally.
  • Upload the workbench.aspx file to Documents library in a developer site collection.

Create a new SharePoint Framework project:

  • Open any command line tool. I preferred to use Cmder.
  • Navigate to a folder or create a new folder
  • Run the Yeoman generator to create SharePoint Framework solution package
  • Fill the details required for generator to create a new SharePoint solution.

    SharePoint Framework Project Generator

    Fig 1: SharePoint Framework Project Generator

  • After getting the Success message,
    We can also run “gulp serve” command to host the application and start the local workbench. The local workbench enables us to test the application in many ways.We can also launch the workbench.aspx file from SharePoint site to test the application in SharePoint site.
  • To include PnP JS library to the project, run the following npm command
    npm i sp-pnp-js –save-dev

    npm command for adding PnP JS library

    Fig 2: npm command for adding PnP JS library

  • The command will include PnP js file and its dependencies to the project folder.

    Successfull installation of PnP js and its dependencies

    Fig 3: Successfull installation of PnP js and its dependencies

Now we have created the solution structure and with the dependencies. Next, we will see on updating the code in the generated project to retrieve the list information and render with custom styles.

Updating the Code:

The below steps and code snippets used to populate the visible lists in SharePoint framework from the SharePoint site.

Navigate to solution folder
Define List Model
Style the DOM structure
Retrieve Mockup information for local workbench
Retrieve actual information from SharePoint site
Render the data based on the environment type (Local / SharePoint)

Navigate to solution folder

    • Open the solution folder in source code editor. I’ll prefer to use Visual Studio Code, because it is a light weight and also available across platforms.

      SharePoint Framework Folder Structure

      Fig 4: SharePoint Framework Folder Structure

    • Expand src > webparts > PnPjsHelloWorld folder and then select PnPjsHelloWorldWebPart.ts file to edit the code.

      WebPart File

      Fig 5: WebPart File

Define List Model

  • Define the interface for List Model in the code. To define that open PnPjsHelloWorldWebPart.ts file.
  • Paste the following interface code just below the PnPJsHelloWorldWebPart class
    export interface ISPList {
      Title: string;
      Id: string;

    The ISPList interface holds the SharePoint lists title and id information

Style DOM structure

SharePoint uses the SaaS language for stylesheets generation. Sass extends CSS language and allows you to use features like variables, nested rules, inline imports in to organize and create efficient style sheets for your web parts. The SharePoint Framework already comes with a SCSS compiler that converts your Sass files to normal CSS files and also provides a typed version to use it in during development.

Follow the to add a styles to the entire list collection and particle list row,

  • Open PnPjsHelloWorld.Module.scss file
  • Add the following styles below to the .button style,
    .list {
      color: #333333;
      font-family: 'Segoe UI Regular WestEuropean', 'Segoe UI', Tahoma, Arial, sans-serif;
      font-size: 14px;
      font-weight: normal;
      box-sizing: border-box;
      margin: 10;
      padding: 10;
      line-height: 50px;
      list-style-type: none;
      box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
    .listItem {
      color: #333333;
      vertical-align: center;
      font-family: 'Segoe UI Regular WestEuropean', 'Segoe UI', Tahoma, Arial, sans-serif;
      font-size: 14px;
      font-weight: normal;
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      box-shadow: none;
      *zoom: 1;
      padding: 9px 28px 3px;
      position: relative;
  • Save the file

After running the gulp build or run tasks, the styles from. scss file are converted to the .css file and generates the corresponding typings in .scss.ts file.SO, we can import the styles and used as a reference in other typescript file.
To import the styles in the web part file by adding below line (Auto generated),

import styles from './PnPjsHelloWorld.module.scss';

By using the below syntax, we can add style references to the typescript files,


Retrieve List info (mock data) from local environment

We have to ensure, local workbench to display some data in the web part to simulate as same as in SharePoint site. For that we have to create mockup data

  • Create a new file “MockHttpClient.ts” inside the src > webparts > PnPjsHelloWorld
  • Copy the following code to the file
    //import ISPList interface to this file from PnPjsHelloWorldWebPart.ts
    import { ISPList } from './PnPjsHelloWorldWebPart';
    //Declare ISPList array and returns the array of items whenever MockHttpClient.get() method called
    export default class MockHttpClient {
        private static _items: ISPList[] = [{ Title: 'Mock List', Id: '1' }];
        public static get(restUrl: string, options?: any): Promise<ISPList[]> {
          return new Promise<ISPList[]>((resolve) =< {
  • Save the file.

Then we have to import the MockHttpClient module in PnPjsHelloWorldWebPart.ts file. So

  • Open PnPjsHelloWorldWebPart.ts file from the editor.
  • Copy the following code just below the line import { IPnPjsHelloWorldWebPartProps } from ‘./IPnPjsHelloWorldWebPartProps’;
    import MockHttpClient from './MockHttpClient';
  • Add the following private method inside the PnPjsHelloWorldWebPart class
      private _getMockListData(): Promise<ISPList[]> {
        return MockHttpClient.get(this.context.pageContext.web.absoluteUrl).then(() => {
            const listData: ISPList[] = [
                    { Title: 'Mock List', Id: '1' },
                    { Title: 'Mock List Two', Id: '2' },
                    { Title: 'Mock List Three', Id: '3' }
            return listData;
        }) as Promise<ISPList[]>;
  • Save the file

Render the retrieved lists information

We have to render two different information based on the workbench environment. For Local workbench, we have to render the mockup data. For SharePoint site workbench, we have to render the actual lists information.

  • To identify the environment, first import the EnvironmentType module to PnPjsHelloWorldWebPart.ts file. So Open PnPjsHelloWorldWebPart.ts file
  • Add the following line to the import section in top of the page.
    import { EnvironmentType } from '@microsoft/sp-client-base';
  • Add the following private method to determine the environment and based on that, call the required get methods to retrieve the array of ISPList collection for appropriate environment. _getMockListData method used to get the mock data and render the items in local environment. _getListData method used to get the actual SharePoint data from SharePoint environment.
    private _renderListAsync(): void {
      // Local environment
      if (this.context.environment.type === EnvironmentType.Local) {
        this._getMockListData().then((response) => {
        }); }
        //SharePoint Site environment
        else {
          .then((response) => {
  • To use the PnP JS component in _getListData method, add the below import statement to the import section in top of the PnPjsHelloWorldWebPart.ts file

    import pnp from 'sp-pnp-js';
  • The below _getListData method, used to get the SharePoint list information using PnP JS core component.

    private _getListData(): Promise<ISPList[]> {
      return pnp.sp.web.lists.filter('Hidden eq false').select('Title,Id').get().then((response) => {
        return response;
  • Add the following method to render the information to the UI based on the retrieved ISPList array collection,
    private _renderList(items: ISPList[]): void {
      let html: string = '';
      items.forEach((item: ISPList) => {
        html += `
        <ul class="${styles.list}">
            <li class="${styles.listItem}">
                <span class="ms-font-l">${item.Title}</span>
      const listContainer: Element = this.domElement.querySelector('#spListContainer');
      listContainer.innerHTML = html;

We can apply the style to the element from another file by importing the css module.
${styles.list} applies the style to the ul element, the style information is retrieved from

  • Modify the WebPart render method contents by replacing with following one,
        this.domElement.innerHTML = `
          <div class="${styles.pnPjsHelloWorld}">
            <div class="${styles.container}">
              <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
                <div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
                  <span class="ms-font-xl ms-fontColor-white">Welcome to SharePoint!</span>
                  <p class="ms-font-l ms-fontColor-white">Customize SharePoint experiences using Web Parts.</p>
                  <p class="ms-font-l ms-fontColor-white">${}</p>
                  <a href="" class="ms-Button ${styles.button}">
                    <span class="ms-Button-label">Learn more</span>
              <div id="spListContainer" />
  • Save the file

Test the application

  • Run “gulp serve” command in command line tool. Which compiles the project and generates the js and css files required for the application.
  • Local workbench automatically started after running the command. The default location for local workbench is http://localhost:4321/temp/workbench.html
    The typescript files and PnP js files are bundled into http://localhost:4321/dist/pn-pjs-hello-world.bundle.js

     Served Files from localhost

    Fig 6: Served Files from localhost

  • Click + icon and Select Web Part Name “PnPjsHelloWorld” to insert the web part to the workbench to test the application. In local workbench, we will get the mockup data in the output.

    Workbench with options

    Fig 7: Workbench with options

  • Open SharePoint site where we have uploaded workbench.aspx file to test the application. Allow unsafe scripts to load in the page to access JavaScript and other dependent files from localhost.

    Local Workbench output with mockup data

    Fig 8: Local Workbench output with mockup data

  • In the workbench page, click + icon and then select the web part name to insert the SharePoint framework web part to the page

    Site Workbench with actual data

    Fig 8: Site Workbench with actual data


In this article, we have learned on how to use a PnP JavaScript library to access SharePoint list collection and render the data in SharePoint Framework web part.