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.
- 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”.
- Then write the comment “require(‘mutationobserver-shim’)” to include mutation observer operations in client-side extensions.
- Update HelloWorldApplicationCustomizer class codes as provided.
Requirement
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 not be 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,
- import { override } from '@microsoft/decorators';
- import { Log } from '@microsoft/sp-core-library';
- import {
- BaseApplicationCustomizer
- } from '@microsoft/sp-application-base';
- import { Dialog } from '@microsoft/sp-dialog';
- require('mutationobserver-shim');
- import * as strings from 'HelloWorldApplicationCustomizerStrings';
- const LOG_SOURCE: string = 'HelloWorldApplicationCustomizer';
-
-
-
-
-
- export interface IHelloWorldApplicationCustomizerProperties {
-
- testMessage: string;
- }
- let _observer: MutationObserver;
-
- export default class HelloWorldApplicationCustomizer
- extends BaseApplicationCustomizer<IHelloWorldApplicationCustomizerProperties> {
- @override
- public onInit(): Promise < void > {
- Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
- let message: string = this.properties.testMessage;
- 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) {
-
- 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"><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) {
- _observer.disconnect();
- }
- console.log('[HelloWorldApplicationCustomizer._onDispose] Disposed custom.');
- }
- }