|
1 |
| - |
2 | 1 | # Build your first SharePoint Framework Extension (Hello World part 1)
|
3 | 2 |
|
4 |
| ->**Note:** The SharePoint Framework Extensions are currently in preview and are subject to change. SharePoint Framework Extensions are not currently supported for use in production environments. |
| 3 | +>**Note:** SharePoint Framework Extensions are currently in preview and are subject to change. They are not currently supported for use in production environments. |
5 | 4 |
|
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. |
| 5 | +SharePoint Framework (SPFx) Extensions are client-side components that run inside the context of a SharePoint page. You can deploy extensions to SharePoint Online, and you can use modern JavaScript tools and libraries to build them. |
7 | 6 |
|
8 |
| -You can also follow these steps by watching the video on the [SharePoint PnP YouTube Channel](https://www.youtube.com/watch?v=0BeS0HukW24&list=PLR9nK3mnD-OXtWO5AIIr7nCR3sWutACpV). |
| 7 | +You can follow the steps in this article by watching the video on the [SharePoint PnP YouTube Channel](https://www.youtube.com/watch?v=0BeS0HukW24&list=PLR9nK3mnD-OXtWO5AIIr7nCR3sWutACpV). |
9 | 8 |
|
10 | 9 | <a href="https://www.youtube.com/watch?v=0BeS0HukW24&list=PLR9nK3mnD-OXtWO5AIIr7nCR3sWutACpV">
|
11 | 10 | <img src="../../../../images/spfx-ext-youtube-tutorial1.png" alt="Screenshot of the YouTube video player for this tutorial" />
|
12 | 11 | </a>
|
13 | 12 |
|
14 | 13 | ## Create an extension project
|
15 |
| -Create a new project directory in your favorite ___location. |
16 | 14 |
|
17 |
| -``` |
18 |
| -md app-extension |
19 |
| -``` |
| 15 | +1. Create a new project directory in your favorite ___location. |
20 | 16 |
|
21 |
| -Go to the project directory. |
| 17 | + ``` |
| 18 | + md app-extension |
| 19 | + ``` |
22 | 20 |
|
23 |
| -``` |
24 |
| -cd app-extension |
25 |
| -``` |
| 21 | +2. Go to the project directory. |
26 | 22 |
|
27 |
| -Create a new HelloWorld extension by running the Yeoman SharePoint Generator. |
| 23 | + ``` |
| 24 | + cd app-extension |
| 25 | + ``` |
28 | 26 |
|
29 |
| -``` |
30 |
| -yo @microsoft/sharepoint |
31 |
| -``` |
| 27 | +3. Create a new HelloWorld extension by running the Yeoman SharePoint Generator. |
32 | 28 |
|
33 |
| -When prompted: |
| 29 | + ``` |
| 30 | + yo @microsoft/sharepoint |
| 31 | + ``` |
34 | 32 |
|
35 |
| -* Accept the default **app-extension** as your solution name and press **Enter**. |
36 |
| -* Choose **Use the current folder** and press **Enter** |
37 |
| -* Choose **N** to require extension to be installed on each site explicitly when it's being used |
38 |
| -* Choose **Extension (Preview)** as the client-side component type to be created. |
39 |
| -* Choose **Application Customizer (Preview)** as the extension type to be created. |
| 33 | +4. When prompted: |
40 | 34 |
|
41 |
| -The next set of prompts will ask for specific information about your extension: |
| 35 | + * Accept the default **app-extension** as your solution name, and press **Enter**. |
| 36 | + * Choose **Use the current folder**, and press **Enter**. |
| 37 | + * Choose **N** to require the extension to be installed on each site explicitly when it's being used. |
| 38 | + * Choose **Extension (Preview)** as the client-side component type to be created. |
| 39 | + * Choose **Application Customizer (Preview)** as the extension type to be created. |
42 | 40 |
|
43 |
| -* Accept the default **HelloWorld** as your extension name and press **Enter**. |
44 |
| -* Accept the default **HelloWorld description** as your extension description and press **Enter**. |
| 41 | +5. The next set of prompts will ask for specific information about your extension. When prompted: |
45 | 42 |
|
46 |
| - |
| 43 | + * Accept the default **HelloWorld** as your extension name, and press **Enter**. |
| 44 | + * Accept the default **HelloWorld description** as your extension description, and press **Enter**. |
47 | 45 |
|
48 |
| -> Notice that if you use too long naming for the extension, that can cause issues. Provided entries are used to generate alias entry for the application customizer manifest json file. If alias is longer than 40 characters, you will have an exception when you are trying to serve the extension using `gulp serve --nobrowser`. You can solve this by updating the alias entry also afterwards. |
| 46 | +  |
49 | 47 |
|
50 |
| -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. |
| 48 | + > **Note:** If you use a name for the extension that is too long, you might encounter issues. The entries provided are used to generate an alias entry for the application customizer manifest json file. If the alias is longer than 40 characters, you will get an exception when you try to serve the extension using `gulp serve --nobrowser`. You can resolve this by updating the alias entry afterward. |
51 | 49 |
|
52 |
| -When the scaffold is complete, you should see the following message indicating a successful scaffold: |
| 50 | + 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. |
53 | 51 |
|
54 |
| - |
| 52 | + When the scaffold is complete, you should see the following message indicating a successful scaffold: |
55 | 53 |
|
56 |
| -For information about troubleshooting any errors, see [Known issues](../basics/known-issues). |
| 54 | +  |
57 | 55 |
|
58 |
| -Once solution scaffolding is completed, type the following into the console to start Visual Studio Code. |
| 56 | + For information about troubleshooting any errors, see [Known issues](../basics/known-issues). |
59 | 57 |
|
60 |
| -``` |
61 |
| -code . |
62 |
| -``` |
| 58 | +6. When the solution scaffolding is complete, type the following into the console to start Visual Studio Code. |
63 | 59 |
|
64 |
| -> 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. |
| 60 | + ``` |
| 61 | + code . |
| 62 | + ``` |
65 | 63 |
|
66 |
| -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. |
| 64 | + >**Note:** 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. |
67 | 65 |
|
68 |
| - |
| 66 | + Notice how the default solution structure looks like the solution structure for client-side web parts. This is the basic SharePoint Framework solution structure, with similar configuration options across all solution types. |
69 | 67 |
|
70 |
| -Open **HelloWorldApplicationCustomizer.manifest.json** at the src\extensions\helloWorld folder. |
| 68 | +  |
71 | 69 |
|
72 |
| -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. |
| 70 | +7. Open **HelloWorldApplicationCustomizer.manifest.json** in the src\extensions\helloWorld folder. |
73 | 71 |
|
74 |
| - |
| 72 | + This file defines your extension type and a unique identifier for your extension. You’ll need this ID later when you debug and deploy your extension to SharePoint. |
75 | 73 |
|
76 |
| -## Coding your Application Customizer |
| 74 | +  |
| 75 | +
|
| 76 | +## Code your Application Customizer |
77 | 77 | Open the **HelloWorldApplicationCustomizer.ts** file in the **src\extensions\helloWorld** folder.
|
78 | 78 |
|
79 | 79 | 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.
|
80 | 80 |
|
81 |
| - |
| 81 | + |
82 | 82 |
|
83 | 83 | The logic for your Application Customizer is contained in the **onInit** method.
|
84 | 84 |
|
85 | 85 | - **onInit()** is called when the client-side extension is first activated on the page. This event occurs after ```this.context``` and ```this.properties``` are assigned. As with web parts, ```onInit()``` returns a promise that you can use to perform asynchronous operations.
|
86 | 86 |
|
87 |
| -> 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. |
| 87 | +>**Note:** The class constructor is called at an early stage, when ```this.context``` and ```this.properties``` are undefined. Custom initiation logic is not supported here. |
| 88 | +
|
| 89 | +The following are the contents of **onInit()** in the default solution. This default solution writes a log to the Dev Dashboard, and then displays a simple JavaScript alert when the page renders. |
88 | 90 |
|
89 |
| -Below is the contents of **onInit()** 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. |
| 91 | + |
90 | 92 |
|
91 |
| - |
| 93 | +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. The default template is looking for a property called **testMessage**. If that property is provided, it outputs it in an alert message. |
92 | 94 |
|
93 |
| -> 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. The default template is looking for a property called testMessage, and if it's provided, outputting it in an alert message. |
| 95 | +## Debug your Application Customizer using gulp serve and query string parameters |
| 96 | +You cannot current use the local workbench to test SharePoint Framework Extensions. You'll need to test them against a live SharePoint Online site. You don't have to deploy your customization to the app catalog to do this, which makes the debugging experience simple and efficient. |
94 | 97 |
|
95 |
| -## Debugging your Application Customizer using gulp serve and query string parameters |
96 |
| -SharePoint Framework extensions cannot currently be tested using the local workbench, so you'll need to test and develop them directly against a live SharePoint Online site. You do not, however, need to deploy your customization to the app catalog to do this, which keeps the debugging experience simple and efficient. |
| 98 | +First, compile your code and host the compiled files from your local computer by running the following command. |
97 | 99 |
|
98 |
| -First, compile your code and host the compiled files from your local machine by running this command: |
99 | 100 | ```
|
100 | 101 | gulp serve --nobrowser
|
101 | 102 | ```
|
102 | 103 |
|
103 |
| ->**Note:** If you do not have the SPFx developer certificate installed, then Workbench will notify you that it is configured not to load scripts from localhost. Stop currently running process in the console window, execute `gulp trust-dev-cert` command in your project directory console to install the developer certificate before running `gulp serve --nobrowser` command again. |
| 104 | +>**Note:** If you do not have the SPFx developer certificate installed, Workbench will notify you that it is not configured to load scripts from localhost. If this happens, stop the process that is currently running in the console window, run the `gulp trust-dev-cert` command in your project directory console to install the developer certificate, and then run the `gulp serve --nobrowser` command again. |
| 105 | +
|
| 106 | +You use the ```--nobrowser``` option because you don't need to launch the local workbench, since you can't debug extensions locally. |
104 | 107 |
|
105 |
| -Notice that we used the ```--nobrowser``` option, since there's no value in launching the local workbench since you currently cannot debug extensions locally. |
| 108 | +When the code compiles without errors, it will serve the resulting manifest from http://localhost:4321. |
106 | 109 |
|
107 |
| -Once it compiles the code without errors, it will serve the resulting manifest from http://localhost:4321. |
| 110 | + |
108 | 111 |
|
109 |
| - |
| 112 | +To test your extension, go to a modern list view page in your SharePoint environment and append the following query string parameters to the URL. Notice that you will need to update the ID to match your own extension identifier. This is available in the **HelloWorldApplicationCustomizer.manifest.json** file. |
110 | 113 |
|
111 |
| -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. Notice that you will need to update the id to match your own extension identifier available from the **HelloWorldApplicationCustomizer.manifest.json** file: |
112 | 114 | ```
|
113 | 115 | ?loadSPFX=true&debugManifestsFile=https://localhost:4321/temp/manifests.js&customActions={"e5625e23-5c5a-4007-a335-e6c2c3afa485":{"___location":"ClientSideExtension.ApplicationCustomizer","properties":{"testMessage":"Hello as property!"}}}
|
114 | 116 | ```
|
115 | 117 |
|
116 | 118 | More detail about the URL query parameters:
|
117 | 119 |
|
118 |
| -* **loadSPFX=true:** ensures that the SharePoint Framework is loaded on the page. For performance reasons, the framework is not normally loaded unless at least one extension is registered. Since no components are registered yet, we must explicitly load the framework. |
| 120 | +* **loadSPFX=true** - Ensures that the SharePoint Framework is loaded on the page. For performance reasons, the framework does not load unless at least one extension is registered. Because no components are registered,you must explicitly load the framework. |
119 | 121 |
|
120 |
| -* **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). |
| 122 | +* **debugManifestsFile** - Specifies that you want to load SPFx components that are locally served. The loader only looks for components in the App Catalog (for your deployed solution) and the SharePoint manifest server (for the system libraries). |
121 | 123 |
|
122 |
| -* **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. |
123 |
| - * **Key:** use the Guid of the extension as the key to associate with the custom action. This has to match on the id value of your extension available from the extension manifest.json. |
124 |
| - * **Location:** the type of custom action, use "ClientSideExtension.ApplicationCustomizer" for the Application Customizer extension |
125 |
| - * **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. |
| 124 | +* **customActions** - Simulates a custom action. When you deploy and register this component in a site, you'll create this **CustomAction** object and describe all the different properties you can set on it. |
| 125 | + * **Key** - Use the Guid of the extension as the key to associate with the custom action. This has to match the ID value of your extension, which is available in the extension manifest.json file. |
| 126 | + * **Location** - The type of custom action. Use "ClientSideExtension.ApplicationCustomizer" for the Application Customizer extension. |
| 127 | + * **Properties** - An optional JSON object that contains properties that will be available via the **this.properties** member. In this HelloWorld example, it defined a ‘testMessage’ property. |
126 | 128 |
|
127 | 129 |
|
128 |
| -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 on the Site Contents page. |
| 130 | +Go to a modern list in SharePoint Online. This can be either a list or a library. Application customizers are also supported in modern pages and on the Site Contents page. |
129 | 131 |
|
130 |
| -Extend the URL with the additional query parameters defined above. Notice that you'll need to update the GUID to match the ID of your custom Application Customizer available from **HelloWorldApplicationCustomizer.manifest.json** at the src\extensions\helloWorld folder. |
| 132 | +Extend the URL with the additional query parameters described. Notice that you'll need to update the GUID to match the ID of your custom Application Customizer. |
131 | 133 |
|
132 |
| -The full URL should look similar to the following depending on your tenant URL: |
| 134 | +The full URL should look similar to the following: |
133 | 135 |
|
134 | 136 | ```
|
135 | 137 | contoso.sharepoint.com/Lists/Contoso/AllItems.aspx?loadSPFX=true&debugManifestsFile=https://localhost:4321/temp/manifests.js&customActions={"e5625e23-5c5a-4007-a335-e6c2c3afa485":{"___location":"ClientSideExtension.ApplicationCustomizer","properties":{"testMessage":"Hello as property!"}}}
|
136 | 138 | ```
|
137 | 139 |
|
138 | 140 | 
|
139 | 141 |
|
140 |
| -Click the "**Load debug scripts**" button to continue loading scripts from your local host. |
| 142 | +Choose "**Load debug scripts**" to continue loading scripts from your local host. |
141 | 143 |
|
142 | 144 | You should now see the alert message on your page.
|
143 | 145 |
|
144 |
| - |
| 146 | + |
145 | 147 |
|
146 |
| -This alert is thrown by your SharePoint Framework Extension. Notice also that since we provided the 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. |
| 148 | +This alert is thrown by your SharePoint Framework Extension. Note that because you provided the **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. |
147 | 149 |
|
148 |
| -> If you are having challenges in getting debugging to work, double check the URL query parameters used for the query. Some browsers tend to encode the parameters and in some scenarios this will impact the behavior. |
| 150 | +> **Note:** If you have issues with debugging, double-check the URL query parameters used for the query. Some browsers encode the parameters and in some scenarios this affects the behavior. |
149 | 151 |
|
150 | 152 | ## Next steps
|
151 |
| -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 placeholders 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 are using the editor). You can continue to let it run while you go to the next article. |
| 153 | +Congratulations, you've gotten your first SharePoint Framework Extension running! To continue building out your extension, see [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 placeholders 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 are using the editor). You can continue to let it run while you go to the next article. |
0 commit comments