Description
Setting up a SharePoint Framework (SPFx) solution and configuring it to work within Visual Studio Code (VS Code) involves several key steps, starting with preparing your environment and then creating the solution itself.
1. Prerequisites and Setup 🛠️
Before creating the SPFx solution, you must have the necessary global tools installed.
- Install Node.js: Download and install the Long-Term Support (LTS) version of Node.js that is compatible with your target SPFx version. The installer typically includes npm (Node Package Manager).
- Install Yeoman and Gulp: These are essential global packages for scaffolding and running your SPFx projects. Open your command prompt/terminal and run:
Bash
npm install -g yo gulp - Install the SharePoint Yeoman Generator: This is the tool that generates the actual SPFx project structure. Run the following command:
Bash
npm install -g @microsoft/generator-sharepoint - Install VS Code: Download and install Visual Studio Code, a lightweight but powerful code editor.
2. Create the SPFx Solution
Use the Yeoman generator to scaffold your SPFx project structure:
- Create a Project Folder: Open your command prompt/terminal and create a directory for your solution, then navigate into it:
Bash
md my-spfx-project cd my-spfx-project - Run the Generator: Execute the SharePoint Yeoman generator:
Bash
yo @microsoft/sharepoint - Answer the Prompts: The generator will ask you a series of questions.
- Solution Name: (Enter your preferred name)
- Target for component: Choose SharePoint Online only (latest) or your desired environment.
- Place of files: Choose Use the current folder.
- Type of client-side component to create: Choose WebPart or Extension.
- Framework to use: Select No JavaScript Framework, React, or Knockout (React is a common choice).
- Open in VS Code: Once scaffolding is complete, open the folder in VS Code:
Bash
code .
3. Configure and Debug in VS Code 💻
To test and debug your solution, you’ll typically use the local SharePoint Workbench.
- Trust the Developer Certificate: SPFx uses a local HTTPS server for development, which requires trusting a self-signed developer certificate. In your VS Code terminal (or a separate command prompt) within the project folder, run:
Bash
gulp trust-dev-cert - Install VS Code Extensions (Optional but Recommended):
- Debugger for Chrome (or Edge): This is often needed for effective debugging within VS Code when running the
gulp servecommand. - SharePoint Framework Toolkit: A helpful extension that provides shortcuts for scaffolding, validation, and deployment.
- Debugger for Chrome (or Edge): This is often needed for effective debugging within VS Code when running the
- Run and Debug Locally:
- Open the integrated terminal in VS Code (View > Terminal).
- Start the local development server:
Bash
gulp serve - This command compiles your solution and opens the SharePoint Workbench in your browser (
https://localhost:4321/temp/workbench.html). - To enable full debugging features (like setting breakpoints) in VS Code, ensure the Debugger for Chrome extension is installed. You can then use the Run and Debug view in VS Code (Ctrl+Shift+D or the play icon) to start debugging and attach the debugger to the browser instance opened by
gulp serve.
There are no reviews yet.