SPFx With SharePoint 2019

Overview

SharePoint Framework (SPFx) is well known for developing the solutions for modern sites in SharePoint Online. Anyway, it isn't just constrained to the cloud. SharePoint Framework development is likewise upheld on SharePoint OnPremise environment (SharePoint 2016 onwards).
In this article, we will explore how we can get our SharePoint 2019 On-Premises environment ready for SharePoint Framework and start developing SPFx client side web parts.
 
 

Get SharePoint Server 2019 SPFx Ready

SharePoint Framework is by default supported in SharePoint Server 2019, without any additional feature patch installations. Follow below steps to make your SP2019 environment ready for SPFx development.
Install NodeJS
  • Install version 8.x from https://nodejs.org
  • If you already have NodeJS installed, check the version
  1. node -v  
Install Code Editor
Install any of below code editor
Install Yeoman and gulp
Run below command
  1. npm install -g yo gulp  
Install Yeoman SharePoint Generator
SPFx version 1.4.1 is supported with SharePoint 2019 (except MS Graph APIs). Run below command.
  1. npm install -g @microsoft/generator-sharepoint  
 

Develop SharePoint Framework Web Part

1. Open command prompt. Create a directory for SPFx solution.
  1. md spfx-sp2019-onprem  
2. Navigate to above created directory.
  1. cd spfx-sp2019-onprem  
3. Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
4. Yeoman generator will present you with the wizard by asking questions about the solution to be created.
 
Solution Name: Hit enter to have default name (spfx-sp2019-onprem in this case) or type in any other name for your solution.
Selected choice: Hit enter (default choice)
 
Target for component: Here we can select the target environment where we are planning to deploy the client webpart i.e. SharePoint Online only (latest) / SharePoint 2016 onwards, including 2019 and SharePoint Online / SharePoint 2019 onwards, including SharePoint Online.
Selected choice: SharePoint 2019 onwards, including SharePoint Online
 
Place of files: We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
 
Deployment option: Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
 
Type of client-side component to create: We can choose to create client side webpart or an extension.
Selected choice: WebPart
 
Web part name: Hit enter to select the default name or type in any other name.
Selected choice: HelloSP2019
 
Web part description: Hit enter to select the default description or type in any other value.
Selected choice: SP2019 development with SPFx
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are No JavaScript Framework, React, and Knockout.
Selected choice: React
 
5. Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take significant amount of time.
6. Once the scaffolding process is completed, lock down the version of project dependencies by running below command.
  1. npm shrinkwrap  
7. In the command prompt type below command to open the solution in code editor of your choice.
  1. code .  
 

SPFx Support for SharePoint 2019

If you use the SharePoint Framework v1.7, you can target scaffolded solution to SharePoint 2019, which will ensure that packages are correct for your target environment.
Open package.json file and observe the SPFx version being used (~1.4.0).
 
 

Test the SPFx WebPart On Local SharePoint Workbench

  1. On the command prompt, type “gulp serve”.
  2. Open SharePoint site.
  3. Navigate to /_layouts/15/workbench.aspx
  4. Add the webpart to page.
 
 
 

Set Up App Catalog

We will set up an App catalog to add the SPFx solution later. Follow below steps as Farm administrator.
1. Open SharePoint 2019 Central Administration site.
2. From left menu, click Apps.
3. Under App Management, click Manage App Catalog.
 
4. Select web application to create the app catalog site.
5. Select Create a new app catalog site.
6. Click OK.
7. Fill in the details to create an App catalog.
 
8. Click OK.
 

Enable Scripting Capabilities

This is an optional step only if you are planning to use SPFx web parts on classic SharePoint sites. If you are going to use SPFx web parts only on Modern SharePoint sites, then you may skip this step.
1. Open SharePoint 2019 Management Shell as an administrator.
2. Execute below PowerShell command.
  1. (Get-SPSite -Identity "http://portal.contoso.com/sites/ModernSite").DenyPermissionsMask = [Microsoft.SharePoint.SPBasePermissions]::EmptyMask  
 

Prepare the Package

We will now prepare a package (.sppkg) to deploy to SharePoint app catalog.
 
Bundle the solution
In the command prompt, type the below command.
  1. gulp bundle --ship  
This command will minify the required assets to “temp\deploy” folder.
 
Package the solution
In the command prompt, type the below command.
  1. gulp package-solution --ship  
This will create the solution package (sppkg) in sharepoint\solution folder.
 

Upload the Package to App Catalog

We will upload the SPFx package to app catalog to make it available on all sites.
1. Open SharePoint App Catalog site.
2. From left menu, click Apps for SharePoint.
3. Click Upload to add SPFx package from “\sharepoint\solution” location.
4. Click OK.
 
5. Click Deploy.
 

Add SPFx Solution to Modern SharePoint Site

We will add SPFx solution to SP2019 Modern SharePoint site.
1. Open SharePoint 2019 Modern SharePoint site.
2. Click cog wheel > Site contents.
 
 
3. Click New > App.
 
 
4. Click to add our SPFx solution to the site.
 
 
 
 

Add SPFx Web Part to Modern Page

Once the SPFx solution is successfully deployed to SharePoint site, we will add web part to modern page.
1. Open existing or create a new modern page.
 
 
2. On the column, click + sign to add a web part to page.
 
 
3. The web part is rendered on the page.
 
 
4. Click F12 to open the developer dashboard and observe the CDN from where the assets are being loaded from.
 
 
 

Summary

For the future compatibility of solution, it is recommended to start using SharePoint Framework based development in SharePoint OnPremise. Also one thing to note that SPFx solutions created for SharePoint Online might not work for SharePoint OnPremise.