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

Given 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 prequisites, 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.

Create a new SharePoint Framework project

  1. Open any command line tool. I prefer to use Cmder.
  2. Navigate to a folder or create a new folder.
  3. Run the Yeoman Generator to create SharePoint Framework solution package.
  4. Fill the details required for creating a new SharePoint solution.

    details

    • 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 the SharePoint site to test the application in SharePoint Framework.

    • To include the PnP JS library to the project, run the following npm command

      npm i sp-pnp-js –save-dev


      command

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

      command

Now, we have created the solution structure and with 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 are used to populate the visible lists in SharePoint Framework from the SharePoint site.

Navigate to solution folder

Define List Model

Style DOM structure

SharePoint uses the SaaS language for stylesheets generation. SaaS extends CSS language and allows you to use features, like variables, nested rules, inline imports in the organized way 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 during the development.

Follow these steps to add styles to the entire list collection and particlular list row.

Retrieve List info (mock data) from local environment

We have to ensure the 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.

Retrieve List info from SharePoint site

Render the retrieved lists information

We have to render two different informations 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.

  1. private _renderListAsync(): void
  2. {
  3. // Local environment
  4. if (this.context.environment.type === EnvironmentType.Local) {
  5. this._getMockListData().then((response) => {
  6. this._renderList(response);
  7. });
  8. }
  9. //SharePoint Site environment
  10. else {
  11. this._getListData().then((response) => {
  12. this._renderList(response);
  13. });
  14. }
  15. }

Test the application

Conclusion

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