Introduction
This is a type of extension using SPFx development, which lets a custom javascript load to every page during the page load. These are client-side components that run inside the context of the SharePoint page. These are similar to delegate controls and script links in the full trust development model. Consider a scenario, where you want to have a banner that has some message. In the initial SharePoint versions, this was primarily achieved using full trust code (which has access to SharePoint system pages and application pages). In modern SharePoint online sites, MSFT allows customization to specific areas
- Top of the page
- Bottom of the page

As you have seen there are very limited options to customize the pages using App Customizer using SharePoint online.
But wait, what can you build using application customizer?
- You can add a header message and a footer message
- Display a welcome greeting to currently logged-in users on first-time login
- Custom Navigation to your site on the header area
- Loading some scripts on page load. This can be an analytics script that tracks the visitors to the site, displaying a disclaimer on page load that forces users to agree to some terms and conditions before accessing the content.
And many more,
In this article, we will simply use the default app customizer has to provide. Let’s see this in action now.
Steps
Step 1. Check the version of SPFx and the toolchain version that is installed on your machine. For this open the command prompt and run the following command.
npm ls -g --depth=0


As seen in the screen capture, for this demo I am running SPFx version 1.14 and node version 14.15.0
Step 2. Have the folder set up where your source code and package are generated. For this demo, I have created under C:\My\SPFx\AppCustomizerDemo

Step 3. Run the Yo SharePoint generator by running the following command.
yo @microsoft/sharepoint
Step 4. It will ask some questions.
- What is your solution name? For this, I left the default.
- What type of client-side component to create? ‘Extension’.
- What type of client-side extension to create? ‘Application Customizer’.
- What is your Application Customizer Name? for this I left the default which is (HelloWorld).

If you observed from the previous SPFx version, no. of questions asked is reduced and now down to 4 only. The rest of the options like isolated permission, and application customizer available to all the sites in the tenant can be configured in application package files (config.json) which will be discussed in upcoming articles.
Step 5. After the project scaffolding is finished by the yeomen generator, examine the source code using the Visual Studio code editor. To open in Visual Studio code just enter code. (observe the space between code and period)

You might be prompted for a security warning, select the ‘Yes I trust the authors’ option, as there are some scripts that are generated from Open-source communities.

Step 6. The main method is present in the src folder (src-extensions-SolutionNameApplicationCustomizer.ts)

Below is the default source code. At first, the main method onInit() gets called.

Step 7. Now go to the serve.json file which is present inside the ‘config’ folder. Please note that starting the version SPFx 1.13 the local workbench is deprecated. Any SPFx solutions need to be tested against the SharePoint workbench. For application customizer solutions it needs to be always tested under SharePoint workbench. Update the page URL in both serveConfiguration and helloWorld entry points. In this case, I am testing against the communication site.
In the serve.json file observe that there are two sections.
- server configuration
- HelloWorld
and each entry points have properties such as page URL and Custom Actions that need to be loaded. For instance, if you want to test the solution against the ‘helloWorld’ entry point use the below command
gulp serve --config = <nameOfEntryPoint>
There could be some instances, where you want to have some test data to be loaded or you want to test solutions with specific URLs, during that time you can create an entry point for testing and update the properties accordingly.

If you want to test against only the helloWorld entry point then you can only change the pageUrl property and run the gulp serve --config=helloWorld.

Serve
The gulp serve command starts the local node server hosting at endpoint http://localhost:4321 and http://localhost:5432. It creates the temporary files in the 'temp and 'dist' folders which are needed for SharePoint workbench to preview the solution during the testing.

Now you will get a pop-up asking to allow debug scripts. Select ‘Load debugs scripts'.

If there are no errors in the solution you should be getting an alert message.

Note the ‘test message for Vinay Comm Site’ is declared inside the ‘helloWorld’ entry point in serve.json.
Step 8. If you just ran gulp serve then the values under the serveConfigurations gets executed. Let’s check it by updating the default properties in serve.json. In this case, I have updated the testMessage property with the value ‘test message with default configuration!!!’.


Select ‘load debugs scripts'.


Step 9. If you are running the SPFx version less than 1.1.3 and you are testing your customization against the modern page, then in order to test the solution you need to construct the URL manually. For serving gulp you need to provide with parameter –nobrowser.



















Join the conversation! Your thoughts help the community grow.