Skip to content

Commit bc363b5

Browse files
committed
Initial tutorials for SPFx Extensions
1 parent d9e955b commit bc363b5

File tree

56 files changed

+1403
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1403
-4
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
# Build your first SharePoint SharePoint Extension (Hello World part 1)
3+
4+
>**Note:** The SharePoint Framework Extensions are currently in preview and is subject to change. SharePoint Framework Extensions are not currently supported for use in production environments.
5+
6+
Extensions are client-side components that run inside the context of a SharePoint page. Extensions can be deployed to SharePoint Online, and you can also use modern JavaScript tools and libraries to build them.
7+
8+
>**Note:** Before following the steps in this article, be sure to [Set up your development environment](../../set-up-your-development-environment). Notice that extensions are currently **ONLY** available from Office 365 developer tenants.
9+
10+
## Create a extension project
11+
Create a new project directory in your favorite ___location.
12+
13+
```
14+
md app-extension
15+
```
16+
17+
Go to the project directory.
18+
19+
```
20+
cd app-extension
21+
```
22+
23+
Create a new HelloWorld extension by running the Yeoman SharePoint Generator.
24+
25+
```
26+
yo @microsoft/sharepoint
27+
```
28+
29+
When prompted:
30+
31+
* Accept the default **app-extension** as your solution name and choose **Enter**.
32+
* Choose **Extension (Preview)** as client-side component type to be created.
33+
* Choose **Application Customizer (Preview)** as they extension type to be created.
34+
35+
The next set of prompts will ask for specific information about your extension:
36+
37+
* Accept the default **HelloWorld** as your extension name and choose **Enter**.
38+
* Accept the default **HelloWorld description** as your extension description and choose **Enter**.
39+
40+
![Yeoman SharePoint generator prompts to create an extension solution](../../../../images/ext-yeoman-app-prompts.png)
41+
42+
At this point, Yeoman will install the required dependencies and scaffold the solution files along with the **HelloWorld** extension. This might take a few minutes.
43+
44+
When the scaffold is complete, you should see the following message indicating a successful scaffold:
45+
46+
![SharePoint client-side solution scaffolded successfully](../../../../images/ext-yeoman-app-complete.png)
47+
48+
For information about troubleshooting any errors, see [Known issues](../basics/known-issues).
49+
50+
Once solution scaffolding is completed, type the following into console to start Visual Studio Code.
51+
52+
```
53+
code .
54+
```
55+
56+
> Notice that because the SharePoint client-side solution is HTML/TypeScript based, you can use any code editor that supports client-side development to build your extension.
57+
58+
Notice how the default solution structure is like the solution structure of client-side web parts. This is the basic SharePoint Framework solution structure, with similar configuration options across all solution types.
59+
60+
![SharePoint Framework solution opened after initial scaffolding](../../../../images/ext-app-vscode-solution-structure.png)
61+
62+
Open **HelloWorldApplicationCustomizer.manifest.json** at the src\extensions\helloWorld folder.
63+
64+
This file defines your extension type and a unique identifier **“id”** for your extension. You’ll need this unique identifier later when debugging and deploying your extension to SharePoint.
65+
66+
![Application customizer manifest json content](../../../../images/ext-app-vscode-manifest.png)
67+
68+
## Coding your Application Customizer
69+
Open the **HelloWorldApplicationCustomizer.ts** file in the **src\extensions\helloWorld** folder.
70+
71+
Notice that base class for the Application Customizer is imported from the **sp-application-base** package, which contains SharePoint framework code required by the Application Customizer.
72+
73+
![import statement for BaseApplicationCustomizer from @microsoft/sp-application-base](../../../../images/ext-app-vscode-app-base.png)
74+
75+
The logic for your Application Customizer is contained in the two methods onInit and onRender.
76+
- **onInit()** is where you should perform any setup needed for your extension. This event occurs after this.context and this.properties are assigned, but before the page DOM is ready. Like with web parts, onInit() returns a promise that you can use to perform asynchronous operations; onRender() will not be called until your promise has resolved. If you don’t need that, simply return super.onInit().
77+
- **onRender()** is where your extension can interact with the UI. This event occurs after the application’s initial page DOM structure has been created (although some parts of the UI may not have finished rendering yet).
78+
79+
> Notice. The class constructor is called at an early stage, when "this.context" and "this.properties" are undefined. We do not support including custom initiation logic here.
80+
81+
Below are the contents of **onInit()** and **onRender()** in the default solution. This default solution simply writes a log to the Dev Dashboard, and then displays a simple JavaScript alert when the page renders.
82+
83+
![default onInit and onRender methods in the code](../../../../images/ext-app-vscode-methods.png)
84+
85+
> If your application customizer uses the ClientSideComponentProperties JSON input, it will be deserialized into the BaseExtension.properties object. You can define an interface to describe it. Default template is looking for property called testMessage and is outputting that in alert message, if it's provided.
86+
87+
## Debugging your Application Customizer using gulp serve and query string parameters
88+
SharePoint Framework extensions cannot be currently tested just by using local workbench, so you'll need to test and develop them directly against live SharePoint Online site. You do not however need to deploy your customization to app catalog, to be able to do this, which will keep the debugging experience simple and efficient.
89+
90+
First, compile your code and host the compiled files from the local machine by running this command:
91+
92+
```
93+
gulp serve --nobrowser
94+
```
95+
Notice that we used the `--nobrowser` option, since there's no value for starting local workbench, since you cannot debug extensions locally currently.
96+
97+
Once it compiles the code without errors, it will serve the resulting manifest from http://localhost:4321.
98+
99+
![gulp serve](../../../../images/ext-app-gulp-serve.png)
100+
101+
To test your extension, navigate to a modern list view page in your SharePoint environment and append the following query string parameters to the URL:
102+
103+
```
104+
?loadSPFX=true&debugManifestsFile=https://localhost:4321/temp/manifests.js&customActions={"d03ae0c2-bbbf-4cf5-9ff7-0986904553da":{"___location":"ClientSideExtension.ApplicationCustomizer","properties":{"testMessage":"Hello as property!"}}}
105+
```
106+
More detail about the URL query parameters:
107+
* **loadSPFX=true:** ensures that the SharePoint Framework is loaded on the page. For performance reasons, the framework normally is not loaded unless at least one extension is registered. Since no components are registered yet, we must explicitly load the framework.
108+
* **debugManifestsFile:** specifies that we want to load SPFx components that are being locally served. Normally the loader only looks for components in the App Catalog (for your deployed solution) and the SharePoint manifest server (for the system libraries).
109+
* **customActions:** this URL query parameter simulates a custom action. When we actually deploy and register this component in a site later in this lab, we’ll create this CustomAction object for real and describe all the different properties you can set on it.
110+
* **Key:** use the Guid of the extension as the key to associate with the custom action
111+
* **Location:** the type of custom action, use "ClientSideExtension.ApplicationCustomizer" for the Application Customizer extension
112+
* **Properties:** an optional JSON object containing properties that will be available via the this.properties member. In this HelloWorld example, it defined a ‘testMessage’ property.
113+
114+
115+
Navigate to a out of the box modern list in SharePoint Online. This can be a list or a library for the initial testing. Application customizers are also supported in modern pages and in Site Contents page.
116+
117+
Extend the URL with the additional query parameters defined above. Notice that you'll need to update the GUID to match ID of your custom Application Customizer available from **HelloWorldApplicationCustomizer.manifest.json** at the src\extensions\helloWorld folder.
118+
119+
Full URL should be looking somewhat follows depending on your tenant URL.
120+
121+
```
122+
contoso.sharepoint.com/Lists/Contoso/AllItems.aspx?loadSPFX=true&debugManifestsFile=https://localhost:4321/temp/manifests.js&customActions={"5fc73e12-8085-4a4b-8743-f6d02ffe1240":{"___location":"ClientSideExtension.ApplicationCustomizer","properties":{"testMessage":"Hello as property!"}}}
123+
```
124+
125+
![Allow Debug Manifest question from the page](../../../../images/ext-app-debug-manifest-message.png)
126+
127+
Click "**Load debug scripts**" button to continue loading scripts from your local host.
128+
129+
You should now see the alert message in your page.
130+
131+
![Allow Debug Manifest question from the page](../../../../images/ext-app-alert-sp-page.png)
132+
133+
This alert is thrown by your SharePoint Framework Extension. Notice also that since we provided testMessage property as part of the debug query parameters, it's included in the alert message. You can configure your extension instances based on the client component properties, which are passed for the instance also in runtime mode.
134+
135+
> If you are having challenges for making the debugging to work, double check the URL query parameters used for the query. Some browsers tend to encode the parameters in some scenarios, which will impact the behavior.
136+
137+
## Next steps
138+
Congratulations on getting your first SharePoint Framework Extension running! Now that your extension is running, you can continue building out your extension in the next topic, [Using page placeholders from Application Customizer (Hello World part 2)](./using-page-placeholder-with-extensions.md). You will use the same project and take advantage of specific content place holders, for modifying the UI of SharePoint. Notice that the `gulp serve` command is still running in your console window (or in Visual Studio Code if you using the editor). You can continue to let it run while you go to the next article.

0 commit comments

Comments
 (0)