{"id":24342,"date":"2020-08-05T15:19:24","date_gmt":"2020-08-05T15:19:24","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=24342"},"modified":"2022-06-13T13:48:42","modified_gmt":"2022-06-13T08:18:42","slug":"how-to-use-xrm-and-formcontext-in-html-webresource-in-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2020\/08\/how-to-use-xrm-and-formcontext-in-html-webresource-in-dynamics-365-crm\/","title":{"rendered":"How to use Xrm and formContext in HTML WebResource in Dynamics 365 CRM"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p style=\"text-align: justify;\">In this blog we will explore how to pass <strong>\u2018Xrm\u2019<\/strong> and <strong>\u2018formContext\u2019<\/strong> in HTML pages using the<br \/>\n<strong>\u2018getContentWindow\u2019<\/strong> client API in Unified Interface of Dynamics 365 CRM so that we can get or set the form attributes inside the HTML web resource. \u00a0As we all know, parent.Xrm is going to deprecate in near future so below is the supported way to use the \u2018Xrm\u2019 and \u2018formContext\u2019 in HTML page.<\/p>\n<p style=\"text-align: justify;\">Suppose there is a HTML web resource on Account entity form which shows notes record related to a Parent Account. And from the HTML page we need to read or set some fields on Account record using formContext and also want to do CRUD operations inside the HTML Page using Xrm.webapi. So, the Xrm and formContext won\u2019t be accessed directly in HTML page. To access, you can follow the below steps:<\/p>\n<p>1. Add the below code in the \u2018onLoad\u2019 event handler of Account entity. The below code is written in Typescript so you need to compile it in JavaScript and add it on \u2018onLoad\u2019 event of the account form.<\/p>\n<p>onLoad(executionContext:any):\u00a0void\u00a0{<\/p>\n<p>try\u00a0{<\/p>\n<p>\/\/validate\u00a0execution\u00a0context<\/p>\n<p>if\u00a0(executionContext\u00a0!=\u00a0undefined\u00a0)<\/p>\n<p>{<\/p>\n<p>\/\/get\u00a0form\u00a0context<\/p>\n<p>let\u00a0formContext\u00a0=\u00a0executionContext.getFormContext();<\/p>\n<p>\/\/validate\u00a0form\u00a0context<\/p>\n<p>if\u00a0(formContext\u00a0!=\u00a0undefined\u00a0)\u00a0{<\/p>\n<p>\/\/Get\u00a0AccountNotes\u00a0control<\/p>\n<p>let\u00a0accountNotesControl\u00a0=\u00a0formContext.getControl(&#8220;WebResource_AccountNotes&#8221;);<\/p>\n<p>\/\/Validate\u00a0control<\/p>\n<p>if\u00a0(accountNotesControl\u00a0!=\u00a0null\u00a0&amp;&amp;\u00a0accountNotesControl\u00a0!=\u00a0undefined)\u00a0{<\/p>\n<p>\/\/get\u00a0getContentWindow<\/p>\n<p>accountNotesControl.getContentWindow().then(<\/p>\n<p>function\u00a0(contentWindow)\u00a0{<\/p>\n<p>\/\/Pass\u00a0xrm\u00a0and\u00a0formcontext<\/p>\n<p>contentWindow.setClientApiContext(Xrm,formContext);<\/p>\n<p>}<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>else{<\/p>\n<p>\/\/Show\u00a0alert<\/p>\n<p>Xrm.Navigation.openAlertDialog({\u00a0title:\u00a0&#8220;Error&#8221;,\u00a0text:\u00a0&#8220;formContext\u00a0is\u00a0undefined&#8221;\u00a0});<\/p>\n<p>}\u00a0}<\/p>\n<p>else{<\/p>\n<p>\/\/Show\u00a0alert<\/p>\n<p>Xrm.Navigation.openAlertDialog({\u00a0title:\u00a0&#8220;Error&#8221;,\u00a0text:\u00a0&#8220;executionContext\u00a0is\u00a0undefined&#8221;\u00a0});<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>catch\u00a0(ex)\u00a0{<\/p>\n<p>\/\/Show\u00a0alert<\/p>\n<p>Xrm.Navigation.openAlertDialog({\u00a0title:\u00a0&#8220;Error&#8221;,\u00a0text:\u00a0ex.message\u00a0});<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p style=\"text-align: justify;\">In the above code, we are accessing the HTML page using getControl method. Using the getContentWindow() which returns a content window instance representing an IFRAME or web resource we are going to pass the parameter \u2018Xrm\u2019 and \u2018formContext\u2019 in HTML page.<\/p>\n<p>\u2018WebResource_AccountNotes\u2019 is the name of the HTML web resource control added on the Account form.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-24344\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/1HTML-WebResource-in-Dynamics-365-CRM.png\" alt=\"HTML WebResource in Dynamics 365 CRM\" width=\"542\" height=\"589\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/1HTML-WebResource-in-Dynamics-365-CRM.png 542w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/1HTML-WebResource-in-Dynamics-365-CRM-276x300.png 276w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/p>\n<p>2. Bind the above code on \u2018Onload\u2019 of the Account form as shown in below screenshot:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-24343\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/2HTML-WebResource-in-Dynamics-365-CRM.png\" alt=\"HTML WebResource in Dynamics 365 CRM\" width=\"604\" height=\"570\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/2HTML-WebResource-in-Dynamics-365-CRM.png 604w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2020\/08\/2HTML-WebResource-in-Dynamics-365-CRM-300x283.png 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/p>\n<p>3. Now, add the following code in your HTML web resource:<\/p>\n<p>&lt;script&gt;<\/p>\n<p>function setClientApiContext(xrm, formContext) {<\/p>\n<p>\/\/To set the attribute on Account form<\/p>\n<p>formContext.getAttribute(&#8220;preferredcontactmethodcode&#8221;).setValue(1);<\/p>\n<p>\/\/To do CRUD operation<\/p>\n<p>var data =<\/p>\n<p>{<\/p>\n<p>&#8220;subject&#8221;: &#8220;Sample&#8221;,<\/p>\n<p>}<\/p>\n<p>\/\/ create Note record<\/p>\n<p>xrm.WebApi.createRecord(&#8220;annotation&#8221;, data).then(<\/p>\n<p>function success(result) {<\/p>\n<p>console.log(&#8220;Notes created with ID: &#8221; + result.id);<\/p>\n<p>},<\/p>\n<p>function (error) {<\/p>\n<p>console.log(error.message);<\/p>\n<p>\/\/ handle error conditions<\/p>\n<p>}<\/p>\n<p>);<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>You can see in above function we have set the value of the \u2018preferredcontactmethodcode\u2019 attribute on Account record and we created a new note record using Xrm.webapi functions.<\/p>\n<p><strong><em>Note:<\/em><\/strong><\/p>\n<p>1. The above blog will only work on Unified Interface.<\/p>\n<p>2. The function name defined in the HTML page i.e. \u2018setClientApiContext\u2019 should use the same function name while calling in Onload function of Form event handler.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>As illustrated above, you can now pass <strong>\u2018Xrm\u2019<\/strong> and <strong>\u2018formContext\u2019<\/strong> in HTML pages using the<br \/>\n<strong>\u2018getContentWindow\u2019<\/strong> client API in Unified Interface of Dynamics 365 CRM.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\"><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/mailchimp-dynamics-365-crm-integration-marketing-4-dynamics\" target=\"_blank\" rel=\"noopener\">Marketing4Dynamics<\/a> \u2013 Mailchimp and Dynamics 365 CRM integration to plan effective sales strategies, increase sales and improve ROI<\/div><\/div><\/h2>\n<ul>\n<li>Sync Audiences, Members and Tags from Mailchimp to CRM<\/li>\n<li>Sync CRM Marketing List (Contacts\/Leads) to Mailchimp<\/li>\n<li>Sync Campaigns and Member activities from Mailchimp to CRM<\/li>\n<li>Monitor and analyze Mailchimp campaign statistics through Dashboards in CRM<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this blog we will explore how to pass \u2018Xrm\u2019 and \u2018formContext\u2019 in HTML pages using the \u2018getContentWindow\u2019 client API in Unified Interface of Dynamics 365 CRM so that we can get or set the form attributes inside the HTML web resource. \u00a0As we all know, parent.Xrm is going to deprecate in near future\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2020\/08\/how-to-use-xrm-and-formcontext-in-html-webresource-in-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":24345,"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":[16],"tags":[2061],"class_list":["post-24342","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","tag-html-webresource-in-dynamics-365-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/24342","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=24342"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/24342\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/24345"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=24342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=24342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=24342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}