{"id":33584,"date":"2023-01-05T16:03:19","date_gmt":"2023-01-05T10:33:19","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=33584"},"modified":"2023-01-13T14:16:50","modified_gmt":"2023-01-13T08:46:50","slug":"use-dom-sanitizer-to-bring-into-effect-rich-text-styling-html-pages-angular-designed-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2023\/01\/use-dom-sanitizer-to-bring-into-effect-rich-text-styling-html-pages-angular-designed-dynamics-365-crm\/","title":{"rendered":"Use DOM Sanitizer to bring into effect Rich Text Styling of HTML pages in Angular designed for Dynamics 365 CRM"},"content":{"rendered":"<h2><strong>Overview<\/strong><\/h2>\n<p>We recently got a requirement from one of our clients whose company is a support based-company. Whenever the emails are received in their CRM using flow, they convert those emails into cases. They wanted the content of these emails to be visible in a section of their related case record.<\/p>\n<p>Our project was in Angular and we got the content of the email properly, but all the styling of the content was not applied in the \u2018innerHtml\u2019 of the Html page. This was because in angular, the styling gets removed from the \u2018innerHtml\u2019.<\/p>\n<p>So, after doing some research, we understood the <strong>Dom Sanitizer<\/strong> concept in Angular, and using that we were able to show the CSS (styling) of the content of the email on the Html page.<\/p>\n<p>Below is the process of how we achieved our requirement by using the <strong>Dom Sanitizer<\/strong> of Angular-<\/p>\n<h2><strong>Implementation<\/strong><\/h2>\n<p>As the content of the email record is in rich-text format and the styling was getting lost, we used Dom-Sanitizer to allow all the styling on our Html page.<\/p>\n<h2><strong>Solution<\/strong><\/h2>\n<p>For using Dom-Sanitizer in <strong>app.component.ts<\/strong>, we have to import its module and add the service module. Below are the imports and the initialization-<\/p>\n<pre class=\"lang:css gutter:true start:1\">import { Component, OnInit, SecurityContext } from '@angular\/core';\r\nimport { DomSanitizer, SafeHtml } from '@angular\/platform-browser';\r\n\r\nimport { Pipe, PipeTransform } from '@angular\/core';\r\n\r\nimport { Subscription } from 'rxjs';\r\n\r\nimport { AppService } from '.\/app.service';\r\n\r\n@Component({\r\n\r\nselector: 'app-root',\r\n\r\ntemplateUrl: '.\/app.component.html',\r\n\r\nstyleUrls: ['.\/app.component.css']\r\n\r\n})\r\n\r\nexport class AppComponent implements OnInit {\r\n\r\ntitle=\"Dom_Sanitizer\";\r\n\r\nretrieveEmailRecordSub = new Subscription();\r\n\r\n_appService: AppService;\r\n\r\nconstructor(private sanitizer: DomSanitizer, appService: AppService) {\r\n\r\nthis._appService = appService;\r\n\r\n}<\/pre>\n<p><strong>Logic: &#8211;<\/strong><\/p>\n<pre class=\"lang:css gutter:true start:1\">ngOnInit(): void {\r\n\r\n\/\/calling the function.\r\n\r\nthis.getContentRichTextEditor();\r\n\r\n}\r\n\r\n\r\n\/\/function to sanitize the values and apply the styling of the content\r\n\r\nsafeHTML(str: string) {\r\n\r\n\/\/this will sanitize the content and will apply the styling that was present in it.\r\n\r\nthis._appService._htmlContent = this.sanitizer.bypassSecurityTrustHtml(str);\r\n\r\nconsole.log(\"htmlcontent\" + this._appService._htmlContent);\r\n\r\n}\r\n\r\n\u00a0\r\n\r\n\/\/function to get the content of email record and display in on the html page.\r\n\r\ngetContentRichTextEditor(): any {\r\n\r\ntry {\r\n\r\n\/\/calling the appservice function to get content of rich text editor field \u00a0of email regarding the case record.\r\n\r\nthis._appService.getEmailRecord();\r\n\r\n\/\/getting the response from the emailRecord function in the app.service.ts file to get the rich-text content of the email\r\n\r\nthis.retrieveEmailRecordSub = this._appService.retrieveEmailRecord$.subscribe(\r\n\r\n(response) =&gt; {\r\n\r\n\/\/validating if the response is right or not\r\n\r\nif (response != undefined &amp;&amp; response.length &gt; 0 &amp;&amp; response != null) {\r\n\r\nthis._appService._sanitizeHtml = response; \/\/ updating the value of rich text into the service variable to use further.\r\n\r\n}\r\n\r\n}\r\n\r\n);\r\n\r\n}\r\n\r\ncatch (error: any) {\r\n\r\nconsole.log(\"Error :- \" + error.message);\r\n\r\n}\r\n\r\n}\r\n\r\n}<\/pre>\n<p>Following is the code of <strong>app.component.html: &#8211;<\/strong><\/p>\n<pre class=\"lang:css gutter:true start:1\">&lt;div&gt;\r\n\r\n&lt;button class=\"btnViewContent\" (click)=\"safeHTML(_appService._sanitizeHtml)\"&gt;\r\n\r\nViewContent\r\n\r\n&lt;\/button&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;div [innerHTML]=\"_appService._htmlContent\"&gt;\r\n\r\n&lt;\/div&gt;<\/pre>\n<p>On the Html page, we used the <strong>htmlContent<\/strong> variable of type <strong>SafeHtml<\/strong> from service to render it inside the innerHtml property in order to display the content on the page. The code of <strong>app.service.ts<\/strong> file was as follows-<\/p>\n<pre class=\"lang:css gutter:true start:1\">export class AppService {\r\n\r\npublic parent: any;\r\n\r\nretrieveEmailRecord$ = new Subject&lt;any&gt;();\r\n\r\n\/\/variable to store the response for sanitizing.\r\n\r\n_sanitizeHtml: string=\"\";\r\n\r\n\/\/vartiable to store the response returned from byPassSecuriyTrustHtml() as SafeHtml so it can be applied in the innerHtml\r\n\r\n_htmlContent: SafeHtml=\"\";\r\n\r\nconstructor() {\r\n\r\n}<\/pre>\n<p>We created the above functionality to get the content of the email from <strong>app.service.ts<\/strong> into a variable so that we could allow the styling and show it on our HTML page.<\/p>\n<p>However, here, we faced an issue, as we were able to get the content of the email body but it was getting displayed on the page after going through <strong>bypassSecurityTrustHtml() <\/strong>function with all the styling not getting applied. So, on close inspection, we realized that the <strong>bypassSecurityTrustHtml() <\/strong>returns a value as<strong> SafeHtml <\/strong>type and in our case, it was getting stored in a string variable. Hence, as a solution, we created a <strong>SafeHtml<\/strong> type variable and used it in <strong>InnerHtml<\/strong> property.<\/p>\n<p>Using the above solution, all the styling got properly applied on the page, and below is the screenshot of the content of the email record on the case entity.<\/p>\n<p><strong> <img decoding=\"async\" class=\"alignnone size-full wp-image-33588\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling.jpeg\" alt=\"Rich Text Styling\" width=\"1423\" height=\"742\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling.jpeg 1423w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling-300x156.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling-1024x534.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling-768x400.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/Rich-Text-Styling-660x344.jpeg 660w\" sizes=\"(max-width: 1423px) 100vw, 1423px\" \/><\/strong><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>As the content of the email record was in rich-text and Angular restricts the CSS(styling) in the innerHtml, the use of Dom Sanitizer helped the CSS (styling) get allowed in the form of SafeHtml and made rendering inside the innerHtml possible.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/click-2-clone-microsoft-dynamics-crm-records\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-33589\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/ClickClone.png\" alt=\"\" width=\"848\" height=\"212\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/ClickClone.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/ClickClone-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/ClickClone-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2023\/01\/ClickClone-660x165.png 660w\" sizes=\"(max-width: 848px) 100vw, 848px\" \/><\/a><\/p>\n<pre class=\"lang:css gutter:true start:1\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overview We recently got a requirement from one of our clients whose company is a support based-company. Whenever the emails are received in their CRM using flow, they convert those emails into cases. They wanted the content of these emails to be visible in a section of their related case record. Our project was in\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2023\/01\/use-dom-sanitizer-to-bring-into-effect-rich-text-styling-html-pages-angular-designed-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,16,18],"tags":[2652,2653],"class_list":["post-33584","post","type-post","status-publish","format-standard","hentry","category-angular","category-dynamics-365","category-dynamics-365-v9-2","tag-angular-designed","tag-rich-text-styling"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=33584"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33584\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=33584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=33584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=33584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}