Mastering SharePoint Framework (SPFx) Development: Introduction to SPFx

In this first article of the series on SharePoint Framework (SPFx) development, we will dive deep into what SPFx is, why it’s an essential tool for modern SharePoint development, and how to get your development environment set up for success. Whether you’re building web parts, extensions, or full-scale SharePoint solutions, understanding SPFx is the foundation upon which everything else is built.

Let’s get started by breaking down the key components and setting up your first project.


What is SharePoint Framework (SPFx)?

The SharePoint Framework (SPFx) is a development model for building web parts and customizations for SharePoint Modern sites. It allows developers to create solutions that integrate seamlessly into the SharePoint user experience, providing a modern, mobile-responsive, and secure approach to custom development.

SPFx development is client-side, meaning it relies on technologies like JavaScript, TypeScript, and HTML. This approach allows for faster development cycles, greater flexibility, and improved performance. Additionally, SPFx web parts run within the context of the current user, meaning they can respect the user’s permissions and access data securely.


Why Use SPFx for SharePoint Development?

SPFx offers several key advantages for modern SharePoint development:

  1. Modern Experience: SPFx allows developers to build web parts that fully integrate with SharePoint’s Modern UI.
  2. Mobile-Ready: Web parts built with SPFx are fully responsive and optimized for mobile use.
  3. Performance: Since SPFx is client-side, it leverages JavaScript to provide fast, efficient page rendering without the need for server-side processing.
  4. Security: Web parts run within the context of the current user, maintaining SharePoint’s native security model.
  5. Customizable: Developers can leverage popular frameworks like React, Angular, or plain JavaScript to build solutions.

Setting Up Your SPFx Development Environment

Before you can start building web parts, you need to set up the necessary tools. Follow these steps to get started.

1. Install Node.js and npm

SPFx is built on top of Node.js, so the first step is installing the latest stable version of Node.js. You’ll also need npm (Node Package Manager), which comes bundled with Node.js.

Download Node.js from the official website: https://nodejs.org/

To verify your installation, run the following commands:

node -v
npm -v

2. Install Yeoman and the SPFx Generator

Yeoman is a tool that scaffolds new projects, and the SharePoint Framework generator (@microsoft/generator-sharepoint) helps you quickly create SPFx solutions.

Run the following commands to install both:

npm install -g yo gulp
npm install -g @microsoft/generator-sharepoint

3. Install a Code Editor

You’ll need a code editor for working with your SPFx project. The recommended editor is Visual Studio Code, which provides great support for TypeScript and JavaScript development.

Download it here: https://code.visualstudio.com/


Creating Your First SPFx Web Part

Now that you have the necessary tools, let’s create your first SPFx web part.

1. Create a New Directory for Your Project

Navigate to a location on your machine where you want to store your project and create a new directory:

mkdir my-first-spfx-webpart
cd my-first-spfx-webpart

2. Run the Yeoman SharePoint Generator

Inside the newly created folder, run the SharePoint generator to scaffold the project:

yo @microsoft/sharepoint

You will be prompted to answer several questions about your project. Here’s a typical setup:

  • What is your solution name?: MyFirstSPFxWebPart
  • Which baseline package do you want to target?: Choose SharePoint Online only (if you’re working with SharePoint Online).
  • Where do you want to place your files?: Use the current folder.
  • Do you want to allow the tenant admin to deploy the solution to all sites?: Select Yes (for tenant-wide deployment).
  • Will the components in the solution require permissions to access web APIs?: No.
  • Which framework would you like to use?: You can choose from React, No JavaScript Framework, or other supported frameworks. For this example, let’s choose React.

The generator will now scaffold the project and install all necessary dependencies.

3. Serve the Project

Once the setup is complete, you can run the project in a local SharePoint workbench:

gulp serve

This will open a local SharePoint Workbench in your browser, where you can preview and test your web part.


Understanding the Project Structure

After the generator completes, you’ll see several files and folders created in your project directory. Here’s an overview of the key parts:

  • src/webparts: Contains the actual code for your SPFx web parts.
  • config: Configuration files for your project, including the Webpack setup.
  • gulpfile.js: Gulp tasks for building and serving your project.
  • package.json: Lists all dependencies and scripts for your project.

At this point, you can start modifying your web part’s code inside the src/webparts directory to customize its functionality and appearance.


Adding Functionality to Your SPFx Web Part

Now that your web part is up and running, let’s add some basic functionality. Since we chose React as the framework, the default web part code includes a simple component.

You can modify the web part by editing the file located at:

src/webparts/myFirstSpfxWebPart/components/MyFirstSpfxWebPart.tsx

For example, let’s update the render method to display a simple welcome message:

import * as React from 'react';
import { IMyFirstSpfxWebPartProps } from './IMyFirstSpfxWebPartProps';

export default class MyFirstSpfxWebPart extends React.Component<IMyFirstSpfxWebPartProps, {}> {
  public render(): React.ReactElement<IMyFirstSpfxWebPartProps> {
    return (
      <div>
        <h1>Welcome to My First SPFx Web Part!</h1>
        <p>This is a simple SharePoint web part built with SPFx and React.</p>
      </div>
    );
  }
}

After saving the changes, your web part will automatically refresh, and you should see the new message in your browser.


Next Steps

Congratulations! You’ve just created your first SharePoint Framework web part. In the next article of this series, we’ll explore PnP (Patterns and Practices), a powerful set of libraries and tools that will help you interact with SharePoint data more efficiently.

Stay tuned for more as we dive deeper into the world of SPFx development, exploring best practices, advanced web parts, extensions, and more.

If you have any questions or comments, feel free to reach out. Let’s build something great with SPFx!


This article introduces the key concepts and tools for getting started with SPFx development. In the next installments of the series, we’ll move into more advanced topics like PnP, Fluent UI, and creating highly interactive and dynamic SharePoint solutions.

Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment