How to use Mutation Observer in SPFx (Application Customizer)

  • Posted by: Softree Technology


The following blog will help you to add your custom action menu in SharePoint modern page. As we are not able to get the DOM element for action menu in OnInit() method of SPFx(application customizer) so here we will see how mutation observer helps to get the DOM element SharePoint modern pages custom action menus and add your own custom action menu to the existing menus.

  1. To use mutation observer in your client-side extension, first, you need to install mutation observer in your solution using the command “npm install MutationObserver-shim”.


2. Then write the comment “require(‘mutationobserver-shim’)”to include mutation observer operations in client-side extensions.


3. Update HelloWorldApplicationCustomizer class codes as provided.


Our requirement was to add our custom personal action menu where we wanted to execute custom JavaScript functions. In oninit method, if we try to get the menus to edit the existing one to add our custom menus then we will be not able to get those default action menus as those menus are not getting rendered in page onload.


For this, we need to use MutationObserver to add our custom menu. Using the following code, I successfully added my custom action menus.

Here mentioned below is the complete typescript file which contains all codes based on the requirement mentioned in this blog

[code lang=”c”]

import { override } from ‘@microsoft/decorators’;
import { Log } from ‘@microsoft/sp-core-library’;
import {
} from ‘@microsoft/sp-application-base’;
import { Dialog } from ‘@microsoft/sp-dialog’;

import * as strings from ‘HelloWorldApplicationCustomizerStrings’;

const LOG_SOURCE: string = ‘HelloWorldApplicationCustomizer’;

* If your command set uses the ClientSideComponentProperties JSON input,
* it will be deserialized into the object.
* You can define an interface to describe it.
export interface IHelloWorldApplicationCustomizerProperties {
// This is an example; replace with your own property
testMessage: string;
let _observer: MutationObserver;
/** A Custom Action which can be run during execution of a Client Side Application */
export default class HelloWorldApplicationCustomizer
extends BaseApplicationCustomizer<IHelloWorldApplicationCustomizerProperties> {

public onInit(): Promise<void> {, `Initialized ${strings.Title}`);

let message: string =;
if (!message) {
message = ‘(No properties were provided.)’;

this.context.placeholderProvider.changedEvent.add(this, this.renderObserver);

return Promise.resolve();

private async renderObserver(): Promise<void> {

if (!_observer) {
// Register observable
let config = { childList: true, subtree: true };
_observer = new MutationObserver(this.addMyCustomMenu);
_observer.observe(document, config);

private addMyCustomMenu(mutationsList) {
var o365Menus = document.querySelectorAll(“*[class^=\”o365cs-mfp-linklist o365cs-segoeRegular o365cs-text-align-left\”]”);
if (o365Menus.length > 0) {
for (var i in o365Menus) {
try {

var menuItem = o365Menus[i];
var innerhtml = menuItem.innerHTML;
var newhtml = ‘<div class=”ms-fcl-tp o365cs-nfd-normal-lineheight”><a class=”ms-fcl-tp o365button” role=”link”‘;
newhtml += ‘href=”javascript:alert(\”Hello\”);” id=”O365_SubLink_CustomMenu”‘;
newhtml += ‘aria-labelledby=”_ariaId_9″ style=””><span class=”_fc_3 owaimg” style=”display: none;”></span>’;
newhtml += ‘<span class=”_fc_4 o365buttonLabel” id=”_ariaId_9″>My Custom Menu</span></a></div>’;

menuItem.innerHTML = innerhtml.concat(newhtml);
} catch (e) { }

private _onDispose(): void {
if (_observer) {
console.log(‘[HelloWorldApplicationCustomizer._onDispose] Disposed custom.’);


This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Author: Softree Technology

Leave a Reply