Skip to content

Commit 04af55c

Browse files
thechriskentVesaJuvonen
authored andcommitted
Minor grammatical updates (SharePoint#675)
1 parent b9fa93c commit 04af55c

File tree

1 file changed

+50
-64
lines changed

1 file changed

+50
-64
lines changed

docs/spfx/web-parts/guidance/use-sp-pnp-js-with-spfx-web-parts.md

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Use sp-pnp-js with SharePoint Framework Web Parts
22

3-
You may opt to use the [sp-pnp-js](https://www.npmjs.com/package/sp-pnp-js) library when building your SharePoint Framework web parts. This library
4-
provides a fluent API to make building your REST queries intuitive and supports batching and caching. You can learn more on the [project's homepage](https://github.com/SharePoint/PnP-JS-Core)
5-
which has links to documentation, samples, and other resources to help you get started.
3+
You may opt to use the [sp-pnp-js](https://www.npmjs.com/package/sp-pnp-js) library when building your SharePoint Framework (SPFx) web parts. This library provides a fluent API to make building your REST queries intuitive and supports batching and caching. You can learn more on the [project's homepage](https://github.com/SharePoint/PnP-JS-Core) which has links to documentation, samples, and other resources to help you get started.
64

75
You can download the [full source](https://github.com/SharePoint/sp-dev-fx-webparts/tree/master/samples/knockout-sp-pnp-js) for this article from the samples site.
86

97
## Setup your Environment
108

11-
Before you can complete this guide you will need to ensure you have [setup your environment](https://dev.office.com/sharepoint/docs/spfx/set-up-your-development-environment) for development
12-
using SharePoint Framework.
9+
Before you can complete this guide, you will need to ensure you have [setup your environment](https://dev.office.com/sharepoint/docs/spfx/set-up-your-development-environment) for development using the SharePoint Framework.
1310

1411
## Create a New Project
1512

@@ -19,30 +16,30 @@ Start by creating a new folder for the project using your console of choice:
1916
md spfx-sp-pnp-js-example
2017
```
2118

22-
And enter that folder
19+
And enter that folder:
2320

2421
```sh
2522
cd spfx-sp-pnp-js-example
2623
```
2724

28-
The run the Yeoman generator for SPFx
25+
Then run the Yeoman generator for SPFx:
2926

3027
```sh
3128
yo @microsoft/sharepoint
3229
```
3330

34-
Enter the following values when prompted during the setup of your new project:
31+
Enter the following values when prompted during the setup of the new project:
3532

3633
- **spfx-sp-pnp-js-example** as solution name (keep default)
3734
- **Current Folder** as the solution ___location
38-
- **Kockout** as the framework
35+
- **Knockout** as the framework
3936
- **SPPnPJSExample** as the name of the web part
4037
- **Example of using sp-pnp-js within SPFx** as the description
4138

4239
![Completed Project Scaffolding](../../../../images/sp-pnp-js-guide-completed-setup.png)
4340

4441

45-
Once the scaffolding completes open the project in your code editor of choice, the screen shots here show [Visual Studio Code](https://code.visualstudio.com/).
42+
Once the scaffolding completes, open the project in the code editor of your choosing. The screenshots shown here demonstrate [Visual Studio Code](https://code.visualstudio.com/). To open the directory within Visual Studio Code, enter the following in the console:
4643

4744
```sh
4845
code .
@@ -52,28 +49,24 @@ code .
5249

5350
## Install And Setup sp-pnp-js
5451

55-
Once your project is created you will need to install and setup sp-pnp-js, starting with installing the package. This section is common for any project type (React, etc).
52+
Once your project is created you will need to install and setup sp-pnp-js, starting with installing the package. These steps are common for any project type (React, etc).
5653

5754
```sh
5855
npm install sp-pnp-js --save
5956
```
6057

61-
Because the sp-pnp-js library constructs REST requests it needs to know the URL to send these requests. When operating within classic sites and pages
62-
we can make use of the global _spPageContextInfo variable. Within SPFx this is not available, or if it is may not be correct. So we need to rely on the
63-
[context](https://dev.office.com/sharepoint/reference/spfx/sp-webpart-base/iwebpartcontext) object
64-
supplied by the framework. There are [two ways](https://github.com/SharePoint/PnP-JS-Core/wiki/Using-sp-pnp-js-in-SharePoint-Framework#establish-context) to ensure you have correctly setup your requests,
65-
we'll use the onInit method in this example.
58+
Because the sp-pnp-js library constructs REST requests it needs to know the URL to send these requests. When operating within classic sites and pages, we can make use of the global `_spPageContextInfo` variable. Within SPFx, this is not available, or if it is, may not be correct. So we need to rely on the [context](https://dev.office.com/sharepoint/reference/spfx/sp-webpart-base/iwebpartcontext) object
59+
supplied by the framework. There are [two ways](https://github.com/SharePoint/PnP-JS-Core/wiki/Using-sp-pnp-js-in-SharePoint-Framework#establish-context) to ensure you have correctly setup your requests, we'll use the `onInit` method in this example.
6660

6761
### Update onInit in SpPnPjsExampleWebPart.ts
6862

69-
Open the src\webparts\spPnPjsExample\SpPnPjsExampleWebPart.ts file and add the import for the pnp root object.
63+
Open the **src\webparts\spPnPjsExample\SpPnPjsExampleWebPart.ts** file and add an import statement for the pnp root object:
7064

7165
```TypeScript
7266
import pnp from "sp-pnp-js";
7367
```
7468

75-
In the onInit method update the code to appear as below. We are adding the block after the super.onInit() call. We do this after the super.onInit to ensure that the framework has a chance to initialize anything
76-
required and that we are setting up the library after those steps are complete.
69+
In the `onInit` method, update the code to appear as below. We are adding the block after the `super.onInit()` call. We do this after the `super.onInit` to ensure that the framework has a chance to initialize anything required and that we are setting up the library after those steps are completed.
7770

7871
```TypeScript
7972
/**
@@ -86,7 +79,7 @@ protected onInit(): Promise<void> {
8679
this._componentElement = this._createComponentElement(tagName);
8780
this._registerComponent(tagName);
8881

89-
// When web part description is changed, notify view model to update.
82+
// When the web part description is changed, notify the view model to update.
9083
this._koDescription.subscribe((newValue: string) => {
9184
this._shouter.notifySubscribers(newValue, 'description');
9285
});
@@ -108,12 +101,9 @@ protected onInit(): Promise<void> {
108101

109102
## Update the ViewModel
110103

111-
Next you will want to replace the contents of the SpPnPjsExampleViewModel.ts file with the below. We are adding an import for the pnp items, an interface to define our list item's fields,
112-
some observables to track both our list of items and new item form, and methods to support getting, adding, & deleting items. We also added an ensureList method which
113-
uses the sp-pnp-js lists.ensure method to always make sure we have the list. There are many ways to provision resources but this was done to demonstrate both creating a list, field,
114-
and items using batching in a single method.
104+
Next, replace the contents of the **SpPnPjsExampleViewModel.ts** file with the code below. We are adding an import statement for the pnp items, an interface to define our list item's fields, some observables to track both our list of items and the new item form, and methods to support getting, adding, & deleting items. We also added an `ensureList` method which uses the sp-pnp-js `lists.ensure` method to always make sure we have the list (and to create it if necessary). There are many ways to provision resources, but this method was choosen to demonstrate creating a list, field, and items using batching within a single method.
115105

116-
The takeaway is that using sp-pnp-js we write much less code to handle the requests and can focus on our business logic.
106+
The takeaway is that by using sp-pnp-js, we write much less code to handle requests and can focus on our business logic.
117107

118108
```TypeScript
119109
import * as ko from 'knockout';
@@ -150,12 +140,12 @@ export default class SpPnPjsExampleViewModel {
150140
constructor(bindings: ISpPnPjsExampleBindingContext) {
151141
this.description(bindings.description);
152142

153-
// When web part description is updated, change this view model's description.
143+
// When the web part description is updated, change this view model's description.
154144
bindings.shouter.subscribe((value: string) => {
155145
this.description(value);
156146
}, this, 'description');
157147

158-
// call the load the items
148+
// Load the items
159149
this.getItems().then(items => {
160150

161151
this.items(items);
@@ -169,7 +159,7 @@ export default class SpPnPjsExampleViewModel {
169159

170160
return this.ensureList().then(list => {
171161

172-
// here we are using the getAs operator so that our returned value will be typed
162+
// Here we are using the getAs operator so that our returned value will be typed
173163
return list.items.select("Id", "Title", "OrderNumber").getAs<OrderListItem[]>();
174164
});
175165
}
@@ -183,20 +173,20 @@ export default class SpPnPjsExampleViewModel {
183173

184174
this.ensureList().then(list => {
185175

186-
// add the new item to the SharePoint list
176+
// Add the new item to the SharePoint list
187177
list.items.add({
188178
Title: this.newItemTitle(),
189179
OrderNumber: this.newItemNumber(),
190180
}).then((iar: ItemAddResult) => {
191181

192-
// add the new item to the display
182+
// Add the new item to the display
193183
this.items.push({
194184
Id: iar.data.Id,
195185
OrderNumber: iar.data.OrderNumber,
196186
Title: iar.data.Title,
197187
});
198188

199-
// clear the form
189+
// Clear the form
200190
this.newItemTitle("");
201191
this.newItemNumber("");
202192
});
@@ -215,30 +205,30 @@ export default class SpPnPjsExampleViewModel {
215205
this.items.remove(data);
216206
});
217207
}).catch((e: Error) => {
218-
alert(`There was an error deleting the item ${e.message}`);
208+
alert(`There was an error deleting the item: ${e.message}`);
219209
});
220210
}
221211
}
222212

223213
/**
224-
* Ensures the list exists and if it creates it adds some default example data
214+
* Ensures the list exists. If not, it creates it and adds some default example data
225215
*/
226216
private ensureList(): Promise<List> {
227217

228218
return new Promise<List>((resolve, reject) => {
229219

230-
// use lists.ensure to always have the list available
220+
// Use lists.ensure to always have the list available
231221
pnp.sp.web.lists.ensure("SPPnPJSExampleList").then((ler: ListEnsureResult) => {
232222

233223
if (ler.created) {
234224

235-
// we created the list on this call so let's add a column
225+
// We created the list on this call, so let's add a column
236226
ler.list.fields.addText("OrderNumber").then(_ => {
237227

238-
// and we will also add a few items so we can see some example data
239-
// here we use batching
228+
// And we will also add a few items so we can see some example data
229+
// Here we use batching
240230

241-
// create a batch
231+
// Create a batch
242232
let batch = pnp.sp.web.createBatch();
243233

244234
ler.list.getListItemEntityTypeFullName().then(typeName => {
@@ -258,9 +248,9 @@ export default class SpPnPjsExampleViewModel {
258248
OrderNumber: "75638923"
259249
}, typeName);
260250

261-
// excute the batched operations
251+
// Excute the batched operations
262252
batch.execute().then(_ => {
263-
// all of the items have been added within the batch
253+
// All of the items have been added within the batch
264254

265255
resolve(ler.list);
266256

@@ -282,7 +272,7 @@ export default class SpPnPjsExampleViewModel {
282272
```
283273
## Update the Template
284274

285-
Finally we need to update the template to match the functionality we have added into the ViewModel. Copy the below code into the SpPnPjsExample.template.html file. We have added a title row, a foreach repeater
275+
Finally, we need to update the template to match the functionality we have added into the ViewModel. Copy the code below into the **SpPnPjsExample.template.html** file. We have added a title row, a foreach repeater
286276
for the items collection, and a form allowing you to add new items to the list.
287277

288278
```html
@@ -344,41 +334,37 @@ for the items collection, and a form allowing you to add new items to the list.
344334
```
345335
## Run the Example
346336

347-
If you now start the sample and add then add the web part to your site's /_layouts/workbench.aspx you can see it in action.
337+
Start the sample and add the web part to your SharePoint hosted workbench (/_layouts/workbench.aspx) to can see it in action.
348338

349339
```sh
350-
gulp serve
340+
gulp serve --nobrowser
351341
```
352342

353343
![Project as it appears on first run](../../../../images/sp-pnp-js-guide-first-run.png)
354344

355-
You can delete existing items by clicking on the trashcan icon, or add items by putting values in both fields and clicking the add button.
345+
You can delete existing items by clicking on the trashcan icon, or add new items by putting values in both fields and clicking the add button.
356346

357347
## Next Steps
358348

359-
The sp-pnp-js library contains a great range of functionality and extensibility. Please check out the [developer guide](https://github.com/SharePoint/PnP-JS-Core/wiki/Developer-Guide) for samples,
360-
guidance, and hints on using and configuring the library.
349+
The sp-pnp-js library contains a great range of functionality and extensibility. Please check out the [developer guide](https://github.com/SharePoint/PnP-JS-Core/wiki/Developer-Guide) for samples, guidance, and hints on using and configuring the library.
361350

362351
## Production Deployment
363352

364-
When you are ready to deploy your solution and want to build using the --ship flag you need to mark sp-pnp-js as an external in
365-
the configuration. This is done by updating the SPFx config/config.js to include this line in the externals section:
353+
When you are ready to deploy your solution and want to build using the `--ship` flag you need to mark sp-pnp-js as an external library in the configuration. This is done by updating the SPFx **config/config.js** file to include this line in the externals section:
366354

367355
```
368356
"sp-pnp-js": "https://cdnjs.cloudflare.com/ajax/libs/sp-pnp-js/2.0.1/pnp.min.js"
369357
```
370358

371-
Here we are using the public CDN but the URL can be internal or any ___location you would like to use. Be sure to update the
372-
version number in the url to match the version you want.
359+
In the configuration above, we are using the public CDN but the URL can be an internal path or any other ___location you would like to use. Be sure, however, to update the version number in the URL to match the version you are targeting.
373360

374361
## Improving the Example - Mock Data
375362

376-
Ideally the sample will work in both the local workbench as well as the SharePoint hosted. To enable this we need to mock our ViewModel and make an update to the web part code as outlined below.
363+
Ideally, the sample should work within both the local workbench as well as the SharePoint hosted workbench. To enable this we need to mock our ViewModel and make an update to the web part code as outlined below.
377364

378365
### Mock ViewModel
379366

380-
Add a new file named MockSpPnPjsExampleViewModel.ts along side the other web part files, then update the content to match the below. This will provide the same set of functionality but does not
381-
rely on SharePoint being available and will work in the local environment.
367+
Add a new file named **MockSpPnPjsExampleViewModel.ts** alongside the other web part files. Update the content of this file using the code below. This will provide the same set of functionality but does not rely on SharePoint being available and will work in the local environment.
382368

383369
```TypeScript
384370
import * as ko from 'knockout';
@@ -403,20 +389,20 @@ export default class MockSpPnPjsExampleViewModel {
403389
constructor(bindings: ISpPnPjsExampleBindingContext) {
404390
this.description(bindings.description);
405391

406-
// When web part description is updated, change this view model's description.
392+
// When the web part description is updated, change this view model's description.
407393
bindings.shouter.subscribe((value: string) => {
408394
this.description(value);
409395
}, this, 'description');
410396

411-
// call the load the items
397+
// Load the items
412398
this.getItems().then(items => {
413399

414400
this.items(items);
415401
});
416402
}
417403

418404
/**
419-
* Gets the items from the list
405+
* Simulates getting the items from the list
420406
*/
421407
private getItems(): Promise<OrderListItem[]> {
422408
return Promise.resolve([{
@@ -437,27 +423,27 @@ export default class MockSpPnPjsExampleViewModel {
437423
}
438424

439425
/**
440-
* Adds an item to the list
426+
* Simulates adding an item to the list
441427
*/
442428
public addItem(): void {
443429

444430
if (this.newItemTitle() !== "" && this.newItemNumber() !== "") {
445431

446-
// add the new item to the display
432+
// Add the new item to the display
447433
this.items.push({
448434
Id: this.items.length,
449435
OrderNumber: this.newItemNumber(),
450436
Title: this.newItemTitle(),
451437
});
452438

453-
// clear the form
439+
// Clear the form
454440
this.newItemTitle("");
455441
this.newItemNumber("");
456442
}
457443
}
458444

459445
/**
460-
* Deletes an item from the list
446+
* Simulates deleting an item from the list
461447
*/
462448
public deleteItem(data): void {
463449

@@ -469,12 +455,12 @@ export default class MockSpPnPjsExampleViewModel {
469455
```
470456
### Update Webpart
471457

472-
Finally we need to update the webpart to use the mock data when appropriate, to do so start by opening the SpPnPjsExampleWebPart.ts file. Start by importing the mock ViewModel web just created.
458+
Finally, we need to update the webpart to use the mock data when appropriate. Open the **SpPnPjsExampleWebPart.ts** file. Start by importing the mock ViewModel web just created:
473459

474460
```TypeScript
475461
import MockSpPnPjsExampleViewModel from './MockSpPnPjsExampleViewModel';
476462
```
477-
Then locate the _registerComponent method and update it as shown below.
463+
Then, locate the `_registerComponent` method and update it as shown below:
478464

479465
```TypeScript
480466
private _registerComponent(tagName: string): void {
@@ -501,7 +487,7 @@ private _registerComponent(tagName: string): void {
501487
}
502488
}
503489
```
504-
Finally type gulp serve in the console to bring up the local workbench, which now will work with the mock data. (If you already have the server running stop it using Ctrl+C and then restart it)
490+
Finally, type `gulp serve` in the console to bring up the local workbench, which now will work with the mock data. (If you already have the server running, stop it using Ctrl+C and then restart it):
505491

506492
```sh
507493
gulp serve
@@ -516,4 +502,4 @@ Remember you can find the full sample [here](https://github.com/SharePoint/sp-de
516502

517503
## Provide Feedback / Report Issues
518504

519-
If you have any feedback or need to report as issue with sp-pnp-js please use the [issues list](https://github.com/SharePoint/PnP-JS-Core/issues) in that repo.
505+
If you have any feedback or need to report an issue with sp-pnp-js, please use the [issues list](https://github.com/SharePoint/PnP-JS-Core/issues) in that repo.

0 commit comments

Comments
 (0)