Build Your First SharePoint Client Side Web Part using SPFx [With Deployment]

If you want to become a SharePoint Framework (SPFx) developer, then you should start by building your first SharePoint client-side web part. This complete tutorial will help you with this. I will explain, step by step, how to develop a SPFx client-side web part, including testing it in the workbench, packaging it, and finally deploying it to the App Catalog site.

By this time, I am assuming you have already set up the SharePoint Framework development environment.

Build a SharePoint Framework Client-Side Web Part

You can build the SPFx web parts using React, No Framework, or even minimal JavaScript frameworks, depending on your project needs. Follow the steps below to create a new SharePoint framework client-side web part.

  1. Open the Command Prompt; you can also use the Node.js Command Prompt.
  2. First, create a folder and navigate to it using the command below.
md SPFXClientWebPartExample

cd SPFXClientWebPartExample
sharepoint framework client web part
  1. Now run the command below.
yo @microsoft/sharepoint

The above command will help us create the SharePoint client-side solution project structure. It will then ask us the following questions and provide answers like those below.

  • What is your solution name? Press Enter to use the default name or type to change the solution name.
  • Which type of client-side component to create? It provides the following options: choose WebPart.
    • WebPart
    • Extension
    • Library
    • Adaptive Card Extension
  • Add new Web part to solution spfx-client-web-part-example.
  • What is your Web part name? By default, it displays “HelloWorld”; I changed it to “ClientWebPartExample” here.
  • Which template would you like to use? It provides the following options: choose No framework
    • Minimal
    • No framework
    • React
build spfx client side web part
  1. It will take some time to create the sharepoint framework client web part. Then, you can see the success message after creating the project, as shown below.
Building Modern SharePoint Web Parts in SPFx
  1. If this is the first time you are developing the client-side web part in SPFx, then run the command below once.
gulp trust-dev-cert
  1. Once the SPFx client web part is created successfully, use the following command to open the SharePoint project using Visual Studio Code.
code . (Code space dot)

Now the SPFx client-side web part folder structure will look like this!

sharepoint framework client web part project structure

Let’s understand the SPFx client web part folder structure in the section below.

Check out PnP React Pagination Control in SharePoint Framework (SPFx) Web Part

SharePoint Framework Web Part Folder Structure

Let me help you understand the SPFx client-side web part folder structure.

The src folder is the most important folder. Within it, you can see our web part (.ts) file, which is selected below. Here, we can add or modify code.

Getting started with creating a no framework Web Part

Below is the code in our web part render method, which you can modify.

  public render(): void {
    this.domElement.innerHTML = `
    <section class="${styles.clientWebPartExample} ${!!this.context.sdks.microsoftTeams ? styles.teams : ''}">
      <div class="${styles.welcome}">
        <img alt="" src="${this._isDarkTheme ? require('./assets/welcome-dark.png') : require('./assets/welcome-light.png')}" class="${styles.welcomeImage}" />
        <h2>Well done, ${escape(this.context.pageContext.user.displayName)}!</h2>
        <div>${this._environmentMessage}</div>
        <div>Web part property value: <strong>${escape(this.properties.description)}</strong></div>
      </div>
      <div>
        <h3>Welcome to SharePoint Framework!</h3>
        <p>
        The SharePoint Framework (SPFx) is a extensibility model for Microsoft Viva, Microsoft Teams and SharePoint. It's the easiest way to extend Microsoft 365 with automatic Single Sign On, automatic hosting and industry standard tooling.
        </p>
        <h4>Learn more about SPFx development:</h4>
          <ul class="${styles.links}">
            <li><a href="https://aka.ms/spfx" target="_blank">SharePoint Framework Overview</a></li>
            <li><a href="https://aka.ms/spfx-yeoman-graph" target="_blank">Use Microsoft Graph in your solution</a></li>
            <li><a href="https://aka.ms/spfx-yeoman-teams" target="_blank">Build for Microsoft Teams using SharePoint Framework</a></li>
            <li><a href="https://aka.ms/spfx-yeoman-viva" target="_blank">Build for Microsoft Viva Connections using SharePoint Framework</a></li>
            <li><a href="https://aka.ms/spfx-yeoman-store" target="_blank">Publish SharePoint Framework applications to the marketplace</a></li>
            <li><a href="https://aka.ms/spfx-yeoman-api" target="_blank">SharePoint Framework API reference</a></li>
            <li><a href="https://aka.ms/m365pnp" target="_blank">Microsoft 365 Developer Community</a></li>
          </ul>
      </div>
    </section>`;
  }

Let’s understand each file usage in this SPFX client-side web part folder structure.

Folder / FileUsage
configThis folder contains configuration files, and we will modify a few files from this folder also.
distThis contains the distributable files, and the typescript files, compiled into JavaScript files, will be in this folder.
libThis folder contains compile-time files.
node_modulesThe node_modules folder contains all the packages and dependencies that your SPFx project needs to run. These dependencies are listed in the package.json file and are installed using the npm install command.
srcThis is where you write your actual solution code, including components, web parts, and custom logic.
tempThis folder contains temporary files used by the SPFx development environment.
.eslintrc.jsThe ESLint configuration file supports disabling or configuring a rule for an entire project. 
.gitignoreThis file instructs Git to ignore certain files.
.npmignoreThis file instructs npm to ignore certain files.
.yo-rc.jsonThis json file contains information on the Yeoman generator used in this project.
gulpfile.jsThis is a gulp configuration file. This file executes the gulp command in this folder.
package-lock.jsonIt ensures consistency, stability, and reproducibility in your project’s dependency management.
package.jsonThis file is used by npm, and it defines the dependencies and their versions.
README.mdThis is the web part documentation file.
tsconcfig.jsonThis file contains TypeScript compilation options.

Check out SPFx Fluent UI React Control: ChoiceGroup and Checkbox

Preview SPFx Client Web Part Locally in SharePoint

Before deploying our SPFx web part, we will test it in our local workbench to ensure everything works as expected. We can test the web part using the SharePoint workbench or directly on a SharePoint site page.

First, let’s see how to test it using the SharePoint workbench.

Load SPFx Web Part in the SharePoint Workbench

To load the web part in a SharePoint workbench, we need to provide our SharePoint site URL in the serve.json file in the config folder. This allows the local server to automatically open the SharePoint workbench url with the web part loaded.

  1. As in the image below, add your SharePoint site URL to the “initialPage” parameter in the serve.json file.
https://szg52.sharepoint.com/sites/SPFXDevelopment/_layouts/workbench.aspx
Test spfx client side web part using sharepoint workbench
  1. Run the following command from the same command prompt or in the Terminal pane of VS Code itself.
gulp serve
Web Part Project Structure in SharePoint Framework (SPFx)
  1. This will open the SharePoint workbench, where you will be able to add the SPFx client-side web part, like below:
build and deploy the client side web part (spfx) in sharepoint online
  1. Once you add it, you can see the web part will look like below:
test spfx web part in local sharepoint workbech url
  1. Any time you want to stop the local workbench, run the command below.
Ctrl + C and click Enter

Now, let’s see how we can test our web part directly on any SharePoint site page in the section below.

Read SharePoint Framework (SPFx) Fluent UI Basic List Example

Run SPFx Web Part Directly on a SharePoint Page

The local workbench is for quick local testing, while the ?debug=true… method allows you to test your local code inside a real SharePoint page, which helps ensure the web part behaves correctly in the actual environment.

Follow the steps below to test the SPFx web part directly in the SharePoint site page.

  1. After running the gulp serve command in the TERMINAL pane. As you can see, I have marked the URL in the image below.
?debug=true&noredir=true&debugManifestsFile=https://localhost:4321/temp/
manifests.js
Debugging SharePoint Framework web parts directly in a site pages
  1. Copy the debug query string shown in the above image and paste it at the end of any SharePoint site page URL, as shown in the image below. Then press Enter. You will see a pop-up window like the one below.
    Choose Load debug scripts to test your SPFx web part on the actual SharePoint page.
spfx test webpart on page
  1. Now, edit the SharePoint site page, click the + icon to add a web part, and under the Advanced section, you can see the web part.
how to test spfx web part in sharepoint online site pages
  1. After clicking the web part, you can publish the page, and then the web part will be present on the site page, as shown below.
SharePoint frame work - test SPFx webparts on modern pages

This way, you can easily test your SPFx web part without deploying. Let’s look at how to make it available in SharePoint Online.

Check out Fluent UI DocumentCard in SPFx Web Part

Package the SPFx Client Side Web Part

Packaging the SPFx solution bundles all the necessary files and configurations into a single .sppkg file. This file is needed to deploy the web part to the SharePoint App Catalog and make it available for use. Let’s see how to package our solution by following the steps below.

  1. In the command prompt or in the Visual Studio Code Terminal, run the following commands one by one.
gulp clean

The above command will delete any previous temporary files created in earlier commands.

  1. Now, run the command below to bundle our client-side solution.
gulp bundle --ship
sharepoint framework client-side web parts
  1. Then, run the following command to package our client-side solution, which contains the web part.
gulp package-solution --ship
deploy spfx client side web part to sharepoint

This will create the package file (.sppkg), which we need to deploy to the SharePoint Online app catalog site.

  1. The .sppkg file will be created in a subdirectory of the project folder, located at /sharepoint/solution. The package file looks like this:
sharepoint framework webpart examples

Now, we need to upload the .sppkg file to the SharePoint App Catalog site. There are two types of app catalogs in SharePoint: the tenant app catalog and the site collection app catalog.

If you want your spfx solution to be available for all SharePoint sites, you can deploy it into the tenant app catalog. If you wish to do it only for a particular site, then deploy it in the site collection app catalog.

In the section below, I will explain how to create a SharePoint site collection app catalog and deploy our SPFx client-side solution into it.

Deploy SPFx Client Side Web Part into SharePoint Site Collection App Catalog

To add an SPFx client web part to your SharePoint site, first, we need to create an App Catalog for the site collection. You can do this using either SharePoint Online Management Shell or PnP PowerShell commands.

Follow the steps below to create a site collection app catalog using SharePoint Online Management Shell commands.

  1. Open the SharePoint Online Management Shell and run the following command to connect with SharePoint.
Connect-SPOService

Then, it will ask you to provide a URL. Please enter your SharePoint admin center URL and press Enter. A login page will then open, where you can provide your credentials.

https://szg52-admin.sharepoint.com
  1. After connecting, run the following command to add the app catalog to the SharePoint site.
Add-SPOSiteCollectionAppCatalog -Site "https://szg52.sharepoint.com/sites/SPFXDevelopment"
create sharepoint site collection app catalog

Once the above command is run, refresh the SharePoint site once, under Site Contents, Apps for SharePoint will be added, as shown in the image below.

sharepoint create site collection app catalog
  1. Now, open the Apps for SharePoint and upload the .sppkg file. A pop-up window will then display to deploy that solution. Select the check mark if you want to make the solution available to all sites; otherwise, leave it unchecked. Then click on Deploy.
enable app catalog sharepoint online site collection powershell
  1. The SPFx client web part is deployed successfully in the site collection app catalog.
how to add client site spfx web part to sharepoint online page

In the next step, we see how to add our newly deployed web part to the SharePoint Online site page.

  1. Open the SharePoint Online modern page, edit the SharePoint page, and then click on the + icon to add a web part to the page. There, you can see our spfx client-side web part will be available.
Deploy your client-side web part to a SharePoint page
  1. Click on the web part to add it to the SharePoint Online modern page.
how to deploy the client side web part spfx in sharepoint online

Here, I explained how to create an SPFx client web part step by step. I also showed the solution folder structure and shared two ways to test the web part without deploying it.

After that, we have seen how to deploy the SPFx client-side web part and add it to a modern SharePoint page. If you are new to SPFx, I hope this article helps you better understand the entire process. Follow the steps to create an SPFx web part, and do let me know in the comments below if you get stuck with any step.

Also, look at some other related blog posts SPFx:

1 thought on “Build Your First SharePoint Client Side Web Part using SPFx [With Deployment]”

Leave a Comment

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App