You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/spfx/web-parts/guidance/migrate-jquery-datatables-script-to-spfx.md
+60-48Lines changed: 60 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,14 @@
1
1
---
2
-
title: Migrate jQuery and DataTables solution built using Script Editor web part to SharePoint Framework
2
+
title: Migrate a jQuery and DataTables solution built using Script Editor web part to the SharePoint Framework
3
3
description: Migrate a SharePoint customization using DataTables to build powerful data overviews of data coming from SharePoint and external APIs.
4
-
ms.date: 08/19/2020
4
+
ms.date: 01/28/2026
5
5
ms.localizationpriority: high
6
6
---
7
7
8
-
# Migrate jQuery and DataTables solution built using Script Editor web part to SharePoint Framework
8
+
# Migrate a jQuery and DataTables solution built using Script Editor web part to the SharePoint Framework
9
9
10
10
One of the frequently used jQuery plug-ins is [DataTables](https://datatables.net/). With DataTables, you can easily build powerful data overviews of data coming from both SharePoint and external APIs.
## List of IT requests built using the Script Editor web part
15
13
16
14
To illustrate the process of migrating a SharePoint customization using DataTables to the SharePoint Framework, use the following solution that shows an overview of IT support requests retrieved from a SharePoint list.
@@ -92,17 +90,17 @@ The solution is built by using the standard SharePoint Script Editor web part. F
@@ -137,6 +135,19 @@ Transforming this customization to the SharePoint Framework offers a number of b
137
135
138
136
### Create new SharePoint Framework project
139
137
138
+
Before You Begin:
139
+
140
+
1. Create a list with columns and sample data:
141
+
142
+
1. Create a SharePoint list named "IT Requests"
143
+
1. Add these columns with exact internal names:
144
+
- BusinessUnit (Text)
145
+
- Category (Text)
146
+
- Status (Choice)
147
+
- DueDate (Date)
148
+
- AssignedTo (Person)
149
+
1. Add sample data to test the solution
150
+
140
151
1. Start by creating a new folder for your project:
141
152
142
153
```console
@@ -167,7 +178,7 @@ Transforming this customization to the SharePoint Framework offers a number of b
167
178
168
179
### Load JavaScript libraries
169
180
170
-
Similar to the original solution built using the Script Editor web part, first you need to load the JavaScript libraries required by the solution. In SharePoint Framework this usually consists of two steps: specifying the URL from which the library should be loaded, and referencing the library in the code.
181
+
Similar to the original solution built using the Script Editor web part, first you need to load the JavaScript libraries required by the solution. In the SharePoint Framework, this usually consists of two steps: specifying the URL from which the library should be loaded, and referencing the library in the code.
171
182
172
183
1. Specify the URLs from which libraries should be loaded. In the code editor, open the **./config/config.json** file, and change the `externals` section to:
173
184
@@ -200,7 +211,7 @@ Similar to the original solution built using the Script Editor web part, first y
200
211
> [!NOTE]
201
212
> For more information on referencing external libraries in SharePoint Framework projects, see [Add an external library to your SharePoint client-side web part](../basics/add-an-external-library.md).
202
213
203
-
1. Open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and after the last `import` statement add:
214
+
1. Open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and after the last `import` statement, add:
204
215
205
216
```typescript
206
217
import 'jquery';
@@ -298,7 +309,7 @@ The next step is to define the Moment.js plug-in for DataTables so that dates in
298
309
);
299
310
```
300
311
301
-
1. For the web part to load the plug-in, it has to reference the newly created **moment-plugin.js**file. In the code editor, open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and after the last `import` statement add:
312
+
1. For the web part to load the plug-in, it has to reference the newly created **moment-plugin.js**file. In the code editor, open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and after the last `import` statement, add:
302
313
303
314
```typescript
304
315
import './moment-plugin';
@@ -314,17 +325,17 @@ The last step is to include the code that initializes the data table and loads t
@@ -338,7 +349,7 @@ The last step is to include the code that initializes the data table and loads t
338
349
```
339
350
340
351
> [!NOTE]
341
-
> Make sure to use internal name (or static name) of columns in`$select` and `$expend` parameters.
352
+
> Make sure to use the internal name (or static name) of columns in`$select` and `$expend` parameters.
342
353
343
354
1. To reference this file in the web part, in the code editor, open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and add `require('./script');` to the end of the `render()`method. The`render()` method should look like the following:
344
355
@@ -369,10 +380,10 @@ The last step is to include the code that initializes the data table and loads t
369
380
1. Verify that the web part is working as expected in the command line by executing:
370
381
371
382
```console
372
-
gulp serve --nobrowser
383
+
heft start --nobrowser
373
384
```
374
385
375
-
Because the web part loads its data from SharePoint, you've to test the web part by using the hosted SharePoint Framework Workbench. Navigate to **https://{your-tenant-name}.sharepoint.com/_layouts/workbench.aspx** and add the web part to the canvas. You should now see the IT requests displayed by using the DataTables jQuery plug-in.
386
+
Because the web part loads its data from SharePoint, you have to test the web part by using the hosted SharePoint Framework Workbench. Navigate to **https://{your-tenant-name}.sharepoint.com/_layouts/workbench.aspx** and add the web part to the canvas. You should now see the IT requests displayed by using the DataTables jQuery plug-in.
376
387
377
388

378
389
@@ -483,17 +494,17 @@ Initially, the name of the list from which the data should be loaded was embedde
@@ -510,16 +521,16 @@ Initially, the name of the list from which the data should be loaded was embedde
510
521
}
511
522
```
512
523
513
-
1. Instead of referencing the code from the **script.js** file, all of its contents are a part of the web part's `render` method. In the REST query, you can now replace the fixed name of the list with the value of the `listName` property that holds the name of the list as configured by the user. Before using the value, it's being escaped by using the lodash's `escape` function to disallow script injection.
524
+
1. Instead of referencing the code from the **script.js** file, all of its contents are a part of the web part's `render`method. In the REST query, you can now replace the fixed name of the list with the value of the `listName` property that holds the name of the list as configured by the user. Before using the value, it's being escaped by using the lodash `escape` function to disallow script injection.
514
525
515
526
At this point, the bulk of the code is still written using plain JavaScript. To avoid build issues with the `$` jQuery variable, you had to define it as `any` type before the class definition. Later, when transforming the code to TypeScript, you replace it with a proper type definition.
516
527
517
-
As you've moved the contents of the **script.js** file into the main web part file, the **script.js** is no longer necessary, and you can delete it from the project.
528
+
As you have moved the contents of the **script.js** file into the main web part file, the **script.js** is no longer necessary, and you can delete it from the project.
518
529
519
530
1. To verify that the web part is working as expected, run the following in the command line:
520
531
521
532
```console
522
-
gulp serve --nobrowser
533
+
heft start --nobrowser
523
534
```
524
535
525
536
1. Navigate to the hosted Workbench and add the web part to the canvas. Open the web part property pane, specify the name of the list with IT requests, and select the **Apply** button to confirm the changes.
@@ -530,7 +541,7 @@ Initially, the name of the list from which the data should be loaded was embedde
530
541
531
542
## Transform the plain JavaScript code to TypeScript
532
543
533
-
Using TypeScript over plain JavaScript offers a number ofbenefits. Not only is TypeScript easier to maintain and refactor, but it also allows you to catch errors earlier. The following steps describe how you would transform the original JavaScript code to TypeScript.
544
+
Using TypeScript over plain JavaScript offers several benefits. Not only is TypeScript easier to maintain and refactor, but it also allows you to catch errors earlier. The following steps describe how you would transform the original JavaScript code to TypeScript.
534
545
535
546
### Add type definitions for used libraries
536
547
@@ -543,7 +554,7 @@ To function properly, TypeScript requires type definitions for the different lib
543
554
```
544
555
545
556
> [!TIP]
546
-
> In this example, we are specifying the exact version of the NPM package we want to install. This will ensure that NPM installs a type declaration package that matches the version of jQuery and the datatables library we are using in our project.
557
+
> In this example, we are specifying the exact version of the NPM package we want to install. This will ensure that NPM installs a type declaration package that matches the version of jQuery and the DataTables library we are using in our project.
547
558
>
548
559
> The `--save-dev` argument tells NPM to save the references to these two packages in the `devDependencies` collection in the **package.json** file. TypeScript declarations are only needed in development, which is why we don't want them in the `dependencies` collection.
549
560
>
@@ -559,7 +570,7 @@ To function properly, TypeScript requires type definitions for the different lib
559
570
560
571
### Update package references
561
572
562
-
To use types from the installed type definitions, you've to change how you reference libraries.
573
+
To use types from the installed type definitions, you have to change how you reference libraries.
563
574
564
575
1. In the code editor, open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and change the `import 'jquery';` statement to:
565
576
@@ -575,7 +586,7 @@ To use types from the installed type definitions, you've to change how you refer
575
586
576
587
### Update main web part files to TypeScript
577
588
578
-
Now that you've type definitions for all libraries installed in the project, you can start transforming the plain JavaScript code to TypeScript.
589
+
Now that you have type definitions for all libraries installed in the project, you can start transforming the plain JavaScript code to TypeScript.
579
590
580
591
1. Define an interface for the IT request information that you retrieve from the SharePoint list. In the code editor, open the **./src/webparts/itRequests/ItRequestsWebPart.ts** file, and just above the web part class, add the following code snippet:
581
592
@@ -612,7 +623,7 @@ Now that you've type definitions for all libraries installed in the project, you
@@ -638,9 +649,9 @@ Now that you've type definitions for all libraries installed in the project, you
638
649
}
639
650
```
640
651
641
-
1. Notice how the AJAX request, to retrieve the data from the SharePoint list, is now typed and helps you ensure you're referring to correct properties when passing them into an array to DataTables. The data structure used by DataTables to represent a row in the table is an array of mixed types, so for simplicity it was defined as `any[]`. Using the `any` type in this context isn't bad, because the data returned inside the `dataSrc` property is used internally by DataTables.
652
+
1. Notice how the AJAX request, to retrieve the data from the SharePoint list, is now typed and helps you ensure you're referring to the correct properties when passing them into an array to DataTables. The data structure used by DataTables to represent a row in the table is an array of mixed types, so for simplicity it was defined as `any[]`. Using the `any` type inthis context isn't bad because the data returned inside the `dataSrc` property is used internally by DataTables.
642
653
643
-
As you're updating the `render()` method, you've also added two more changes. First, you removed the `id` attribute from the table. This allows you to place multiple instances of the same web part on the page. Also, you removed the reference to the `$(document).ready()`function, which isn't necessary because the DOM of the element where the data table is rendered is set before the DataTables initiation code.
654
+
As you're updating the `render()` method, you have also added two more changes. First, you removed the `id` attribute from the table. This allows you to place multiple instances of the same web part on the page. Also, you removed the reference to the `$(document).ready()`function, which isn't necessary because the DOM of the element where the data table is rendered is set before the DataTables initiation code.
644
655
645
656
### Update the Moment.js DataTables plugin to TypeScript
646
657
@@ -651,7 +662,7 @@ The last piece of the solution that needs to be transformed to TypeScript is the
651
662
652
663
```typescript
653
664
import * as $ from 'jquery';
654
-
import * as moment from 'moment';
665
+
import moment from 'moment';
655
666
656
667
/* tslint:disable:no-function-expression */
657
668
($.fn.dataTable.renderasany).moment = function (from: string, to: string, locale: string): (d: any, type: string, row: any) => string {
@@ -675,11 +686,12 @@ The last piece of the solution that needs to be transformed to TypeScript is the
675
686
};
676
687
```
677
688
678
-
1. You start with loading references to jQuery and Moment.js to let TypeScript know what the corresponding variables refer to. Next, you define the plug-in function. Usually in TypeScript you use the arrow notation for functions (`=>`). In this case, however, because you need access to the `arguments` property, you've to use the regular function definition. To prevent tslint from reporting a warning about not using the arrow notation, you can explicitly disable the `no-function-expression` rule around the function definition.
689
+
1. You start by loading references to jQuery and Moment.js to let TypeScript know what the corresponding variables refer to. Next, you define the plug-infunction. Usually, in TypeScript, you use the arrow notation for functions (`=>`). In this case, however, because you need access to the `arguments` property, you have to use the regular function definition. To prevent tslint from reporting a warning about not using the arrow notation, you can explicitly disable the `no-function-expression` rule around the function definition.
679
690
1. To confirm that everything is working as expected, in the command line, execute:
680
691
681
692
```console
682
-
gulp serve --nobrowser
693
+
heft start --nobrowser
683
694
```
684
695
685
696
1. Navigate to the hosted Workbench and add the web part to the canvas. Although visually nothing has changed, the new code base uses TypeScript and its type definitions to help you maintain the solution.
0 commit comments