Exercise - Interact with SPFx client-side web parts in modern sites

Completed

In this exercise, you'll add and interact with SharePoint Framework-based client-side web parts in a SharePoint Online modern site collection.

Open a browser and navigate to the developer site you created in the first exercise. If prompted, sign in using your Work or School credentials.

Select the Pages link in the left-hand Quick Launch navigation menu.

Screenshot of the SharePoint site's Quick Launch navigation with the Pages library highlighted

In the Pages library, select the New button from the toolbar and select Site Page to create a new page.

Screenshot of creating a new modern site page in the Pages library

Set the name of the page to Getting Started.

Screenshot of the page name

Select the web part icon button to open the list of available web parts.

Screenshot of the web part icon on the page

Select the web part Text.

Screenshot of the web part toolbox

When the web part is added to the page, add some text and use the rich-text formatting tools provided in the toolbar.

Screenshot of the web part on the page

Select Save as draft to see the page as your users will see it.

Screenshot of saving the page

Screenshot of the draft page

Verify your developer environment is properly configured

Note

The instructions below assume you're using v1.17.1 of the SharePoint Framework Yeoman generator. For more information on the use of the SharePoint Framework Yeoman generator, see Yeoman generator for the SharePoint Framework

Use the tools installed in the second exercise to create a new SharePoint Framework component and test it using the SharePoint Online hosted workbench.

Open a command prompt and change to an empty folder where you want to store the files for the project.

Run the SharePoint Framework Yeoman generator by executing the following command:

yo @microsoft/sharepoint

Use the following to complete the prompt that is displayed:

  • What is your solution name?: HelloWorld
  • Which type of client-side component to create?: WebPart
  • What is your Web part name?: HelloWorld
  • Which template would you like to use?: No framework

After provisioning the folders required for the project, the generator will install all the dependency packages using npm.

When npm completes downloading all dependencies, install the developer certificate by executing the following command:

gulp trust-dev-cert

Run the project by executing the following command:

gulp serve --nobrowser

The SharePoint Framework's gulp serve task with the nobrowser switch will build the project and start a local web server.

Wait for the reload subtask to finish executing. At this point, the web part will be ready for testing.

Screenshot of the gulp serve output

Open a browser and navigate to the developer site you created in the first exercise. If prompted, sign in using your Work or School credentials. Append the following to the end of the site's URL: /_layouts/workbench.aspx. This is the SharePoint Online hosted workbench.

Screenshot of the SharePoint Workbench

Select the web part icon button to open the list of available web parts and select the HelloWorld web part from the Local section.

Screenshot of adding the HelloWorld web part

Edit the web part's properties by selecting the pencil (edit) icon in the toolbar to the left of the web part.

Screenshot of the web part edit toolbar

In the property pane that opens, change the value of the Description Field. Notice how the web part updates as you make changes to the text.

Screenshot of editing the web part property pane

Close the browser and stop the local web server by pressing CTRL+C in the command prompt.

Summary

In this exercise, you added and interacted with SharePoint Framework-based client-side web parts in a SharePoint Online modern site collection.

Test your knowledge

1.

What purpose does Yeoman serve in the build toolchain?

2.

What purpose does the Yeoman generator serve in the build toolchain?