Open the command prompt. Create a directory for SPFx solution.
Navigate to the above-created directory.
Run Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
Selected choice: Hit enter
Target for the component: Here, we can select the target environment where we are planning to deploy the client webpart; i.e. SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
Place of files: We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
Deployment option: Selecting Y will allow the app to deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
Type of client-side component to create: We can choose to create a client-side webpart or an extension. Choose the webpart option.
Selected choice: WebPart
Web part name: Hit enter to select the default name or type in any other name.
Selected choice: AzureFunctionWebPart
Web part description: Hit enter to select the default description or type in any other value.
Selected choice: Call Azure Function from SPFx
Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice: No JavaScript Framework
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command
In the command prompt type the below command to open the solution in the code editor of your choice.
Code the webpart
- Open AzureFunctionWebPartWebPart.ts under “\src\webparts\azureFunctionWebPart\” folder
- Add the below imports
- import { HttpClient, SPHttpClient, HttpClientConfiguration, HttpClientResponse, ODataVersion, IHttpClientConfiguration, IHttpClientOptions, ISPHttpClientOptions } from '@microsoft/sp-http';
- Implement method callAzureFunction as below,
- export default class AzureFunctionWebPartWebPart extends BaseClientSideWebPart<IAzureFunctionWebPartWebPartProps> {
- protected functionUrl: string = "https://spfxcaller.azurewebsites.net/api/HttpTrigger";
- protected callAzureFunction(): void {
- const requestHeaders: Headers = new Headers();
- requestHeaders.append("Content-type", "text/plain");
- requestHeaders.append("Cache-Control", "no-cache");
-
- var siteUrl: string = this.context.pageContext.web.absoluteUrl;
- var userName: string = (<HTMLInputElement>document.getElementById("txtUserName")).value;
- console.log(`SiteUrl: '${siteUrl}', UserName: '${userName}'`);
- const postOptions: IHttpClientOptions = {
- headers: requestHeaders,
- body: `{ name: '${userName}' }`
- };
- let responseText: string = "";
- let resultMsg: HTMLElement = document.getElementById("responseContainer");
- this.context.httpClient.post(this.functionUrl, HttpClient.configurations.v1, postOptions).then((response: HttpClientResponse) => {
- response.json().then((responseJSON: IData) => {
-
- if (response.ok) {
- resultMsg.style.color = "white";
- } else {
- resultMsg.style.color = "red";
- }
-
- resultMsg.innerText = responseJSON.name;
- })
- .catch ((response: any) => {
- let errMsg: string = `WARNING - error when calling URL ${this.functionUrl}. Error = ${response.message}`;
- resultMsg.style.color = "red";
- console.log(errMsg);
- resultMsg.innerText = errMsg;
- });
- });
- }
- Update render() method as below,
- public render(): void {
- this.domElement.innerHTML = `
- <div class="${ styles.azureFunctionWebPart }">
- <div class="${ styles.container }">
- <div class="${ styles.row }">
- <div class="${ styles.column }">
- <span class="${ styles.title }">Call Azure Function</span>
- <p class="${ styles.subTitle }">Customize SharePoint experiences using Web Parts.</p>
-
- <div class="${styles.row}">
- <span class="ms-font-l ms-fontColor-white ${styles.label}">User name:</span>
- <input type="text" id="txtUserName"></input>
- </div>
-
- <button id="btnCallAzureFunction" class="${styles.button}">Say Hello!</button>
- <div id="responseContainer" class="${styles.label}"></div>
-
- </div>
- </div>
- </div>
- </div>`;
-
- document.getElementById("btnCallAzureFunction").onclick = this.callAzureFunction.bind(this);
- }
Test the WebPart
- On the command prompt, type “gulp serve”
- Open SharePoint site
- Navigate to /_layouts/15/workbench.aspx
- Add the webpart to page.