SharePoint Framework - Integrating jQuery With SPFx Web Parts

Overview

SharePoint Framework client-side web parts are developed using JavaScript framework. jQuery is one of the largely used JavaScript frameworks.

In this article, we will explore how to integrate jQuery with SPFx web parts.

 
Create SPFx Solution

Create a directory for SPFx solution.
  1. md spfx-jqueryintegration  
Navigate to the above-created directory.
  1. cd spfx-jqueryintegration  
Run Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.

SharePoint  
 
Solution Name

Hit Enter to have the default name (spfx-jqueryintegration in this case) or type in any other name for your solution.

Selected choice - Hit Enter.
 
Target for component

Here, we can select the target environment where we are planning to deploy the client webpart, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).

Selected choice - SharePoint Online only (latest).
 
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. Choose the webpart option.

Selected choice - WebPart
 
Web part name

Hit enter to select the default name or type in any other name.

Selected choice - SPFxJqueryIntegration
 
Web part description

Hit enter to select the default description or type in any other value.

Selected choice - Hit enter (default description)
 
Framework to use

Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)

Selected choice - No JavaScript Framework. We will integrate jQuery to this solution.
 
Configure Required Packages and Dependencies 

Include Packages

We will now include the JQuery packages. Type in the below commands on the command prompt.
  1. npm i jquery jqueryui combokeys --save  
The --save option enables NPM to include the packages to dependencies section of the package.json file.
 
Include Typings

Typings will help for auto complete while writing the code in the code editor. Type the below command.
  1. tsd install jquery jqueryui combokeys --save   
Lock down the package dependencies

Type in below command to lock down the package dependencies.
  1. npm shrinkwrap  
Solution Changes

In the command prompt, type the below command to open the solution in code editor of your choice.
  1. code .  
Expand node_module folder to see the npm packages being added for jQuery.

SharePoint  
 
Open config.json under config folder. Under externals node, add jQuery references.

SharePoint  
 
Open package.json and verify jQuery dependencies are listed.
 
SharePoint  
 
Open webpart file SpFxJQueryIntegrationWebPart.ts and import jQuery.
 
SharePoint  
 
 Define a constructor and load external jQuery UI CSS from it.
 
SharePoint  
 
Right click on the spFxJQueryIntegration folder, click New File.
 
SharePoint  
 
 Name the file as AccordianTemplate.ts and add the below content.
 
SharePoint  
 
 Use the accordion template in the main webpart class.
 
SharePoint  
 
Test the web part
  1. On the command prompt, type gulp serve
  2. Open any SharePoint site in your tenant or use SharePoint local workbench.
  3. Add the App to your site from “Add an App” menu.
  4. Edit any page and add the webpart.

    SharePoint

  5. See the webpart working.

    SharePoint
Summary

JavaScript frameworks like jQuery can be easily integrated with SPFx client web parts. They also support typing which helps IntelliSense while developing the code in editors.