{"id":820,"date":"2019-01-04T10:59:25","date_gmt":"2019-01-04T10:59:25","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=820"},"modified":"2019-01-04T10:59:25","modified_gmt":"2019-01-04T10:59:25","slug":"use-mutation-observer-spfx-application-customizer","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/","title":{"rendered":"How to use Mutation Observer in SPFx (Application Customizer)"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>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.<\/p>\n<ol>\n<li>To use mutation observer in your client-side extension, first, you need to install mutation observer in your solution using the command \u201cnpm install MutationObserver-shim\u201d.<\/li>\n<\/ol>\n<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-821\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png\" alt=\"1\" width=\"1076\" height=\"239\" \/><\/a><\/p>\n<p>2. Then write the comment \u201crequire(&#8216;mutationobserver-shim&#8217;)\u201dto include mutation observer operations in client-side extensions.<\/p>\n<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-822\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/2.png\" alt=\"2\" width=\"1136\" height=\"484\" \/><\/a><\/p>\n<p>3. Update HelloWorldApplicationCustomizer class codes as provided.<\/p>\n<p><strong>Requirement:-<\/strong><\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-823\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/3.png\" alt=\"3\" width=\"1053\" height=\"451\" \/><\/a><\/p>\n<p>For this, we need to use MutationObserver to add our custom menu. Using the following code, I successfully added my custom action menus.<\/p>\n<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-824\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/4.png\" alt=\"4\" width=\"1053\" height=\"487\" \/><\/a><br \/>\nHere mentioned below is the complete typescript file which contains all codes based on the requirement mentioned in this blog<\/p>\n<p>[code lang=&#8221;c&#8221;]<\/p>\n<p>import { override } from &#8216;@microsoft\/decorators&#8217;;<br \/>\nimport { Log } from &#8216;@microsoft\/sp-core-library&#8217;;<br \/>\nimport {<br \/>\nBaseApplicationCustomizer<br \/>\n} from &#8216;@microsoft\/sp-application-base&#8217;;<br \/>\nimport { Dialog } from &#8216;@microsoft\/sp-dialog&#8217;;<br \/>\nrequire(&#8216;mutationobserver-shim&#8217;);<\/p>\n<p>import * as strings from &#8216;HelloWorldApplicationCustomizerStrings&#8217;;<\/p>\n<p>const LOG_SOURCE: string = &#8216;HelloWorldApplicationCustomizer&#8217;;<\/p>\n<p>\/**<br \/>\n* If your command set uses the ClientSideComponentProperties JSON input,<br \/>\n* it will be deserialized into the BaseExtension.properties object.<br \/>\n* You can define an interface to describe it.<br \/>\n*\/<br \/>\nexport interface IHelloWorldApplicationCustomizerProperties {<br \/>\n\/\/ This is an example; replace with your own property<br \/>\ntestMessage: string;<br \/>\n}<br \/>\nlet _observer: MutationObserver;<br \/>\n\/** A Custom Action which can be run during execution of a Client Side Application *\/<br \/>\nexport default class HelloWorldApplicationCustomizer<br \/>\nextends BaseApplicationCustomizer&lt;IHelloWorldApplicationCustomizerProperties&gt; {<\/p>\n<p>@override<br \/>\npublic onInit(): Promise&lt;void&gt; {<br \/>\nLog.info(LOG_SOURCE, `Initialized ${strings.Title}`);<\/p>\n<p>let message: string = this.properties.testMessage;<br \/>\nif (!message) {<br \/>\nmessage = &#8216;(No properties were provided.)&#8217;;<br \/>\n}<\/p>\n<p>this.context.placeholderProvider.changedEvent.add(this, this.renderObserver);<\/p>\n<p>return Promise.resolve();<br \/>\n}<\/p>\n<p>private async renderObserver(): Promise&lt;void&gt; {<\/p>\n<p>if (!_observer) {<br \/>\n\/\/ Register observable<br \/>\nlet config = { childList: true, subtree: true };<br \/>\n_observer = new MutationObserver(this.addMyCustomMenu);<br \/>\n_observer.observe(document, config);<br \/>\n}<br \/>\n}<\/p>\n<p>private addMyCustomMenu(mutationsList) {<br \/>\nvar o365Menus = document.querySelectorAll(&#8220;*[class^=\\&#8221;o365cs-mfp-linklist o365cs-segoeRegular o365cs-text-align-left\\&#8221;]&#8221;);<br \/>\nif (o365Menus.length &gt; 0) {<br \/>\nfor (var i in o365Menus) {<br \/>\ntry {<\/p>\n<p>var menuItem = o365Menus[i];<br \/>\nvar innerhtml = menuItem.innerHTML;<br \/>\nvar newhtml = &#8216;&lt;div class=&#8221;ms-fcl-tp o365cs-nfd-normal-lineheight&#8221;&gt;&lt;a class=&#8221;ms-fcl-tp o365button&#8221; role=&#8221;link&#8221;&#8216;;<br \/>\nnewhtml += &#8216;href=&#8221;javascript:alert(\\&#8221;Hello\\&#8221;);&#8221; id=&#8221;O365_SubLink_CustomMenu&#8221;&#8216;;<br \/>\nnewhtml += &#8216;aria-labelledby=&#8221;_ariaId_9&#8243; style=&#8221;&#8221;&gt;&lt;span class=&#8221;_fc_3 owaimg&#8221; style=&#8221;display: none;&#8221;&gt;&lt;\/span&gt;&#8217;;<br \/>\nnewhtml += &#8216;&lt;span class=&#8221;_fc_4 o365buttonLabel&#8221; id=&#8221;_ariaId_9&#8243;&gt;My Custom Menu&lt;\/span&gt;&lt;\/a&gt;&lt;\/div&gt;&#8217;;<\/p>\n<p>menuItem.innerHTML = innerhtml.concat(newhtml);<br \/>\n} catch (e) { }<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>private _onDispose(): void {<br \/>\nif (_observer) {<br \/>\n_observer.disconnect();<br \/>\n}<br \/>\nconsole.log(&#8216;[HelloWorldApplicationCustomizer._onDispose] Disposed custom.&#8217;);<br \/>\n}<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p><strong>This solution is brought to you by our SharePoint professionals.<\/strong><\/p>\n<p><a href=\"http:\/\/www.softreeconsulting.com\/\">Softree Consulting<\/a>\u00a0employs 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.<\/p>\n<p>Be it <a href=\"http:\/\/www.softreeconsulting.com\/sharepoint-spfx\/\">SPFx or SharePoint <\/a>add-in developments,\u00a0SharePoint 2019 developments, web part developments, migrating from SharePoint 2010\/2013 to SharePoint 2013\/2016\/Office 365, Office 365,\u00a0SharePoint hosted apps development\u00a0or something else in SharePoint, we strive to deliver the best<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[203,204,205,206,207],"class_list":["post-820","post","type-post","status-publish","format-standard","hentry","category-sharepoint","tag-add-custom-menu-in-sharepoint-modern-pages","tag-how-to-use-mutation-observer-in-spfx","tag-mutation-observer-in-sharepoint-modern-pages","tag-mutation-observer-in-spfx","tag-npm-install-mutationobserver-shim"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use Mutation Observer in SPFx (Application Customizer) - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Mutation Observer in SPFx (Application Customizer) - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"&nbsp; 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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-04T10:59:25+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use Mutation Observer in SPFx (Application Customizer) - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/","og_locale":"en_US","og_type":"article","og_title":"How to use Mutation Observer in SPFx (Application Customizer) - Softree Technology","og_description":"&nbsp; 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 [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/","og_site_name":"Softree Technology","article_published_time":"2019-01-04T10:59:25+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"How to use Mutation Observer in SPFx (Application Customizer)","datePublished":"2019-01-04T10:59:25+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/"},"wordCount":611,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png","keywords":["Add custom menu in SharePoint modern pages","how to use mutation observer in spfx","mutation observer in SharePoint modern pages","mutation observer in spfx","npm install mutationobserver-shim"],"articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/","name":"How to use Mutation Observer in SPFx (Application Customizer) - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png","datePublished":"2019-01-04T10:59:25+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#primaryimage","url":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png","contentUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2019\/01\/1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/use-mutation-observer-spfx-application-customizer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use Mutation Observer in SPFx (Application Customizer)"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=820"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/820\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}