CRM Crate

How to pass the Form Context in HTML web resource of Dynamics 365?

We will learn to pass the Form Context in HTML web resource of Dynamics 365 CRM. Before we start, make sure to subscribe to CRM Crate so that you stay up to date in the field of Dynamics 365 CRM.

How to pass the Form Context in HTML web resource of Dynamics 365 CRM?

Why do we need to pass a Execution Context in HTML web resource?

Consider a business scenario where a HTML page is hosted on an entity form and we need to perform an operation in the HTML script which targets the current Dynamics 365 entity form. We will be needing an execution context or a form’s context for performing any operation on an entity form on which the HTML web resource / page is deployed or hosted.

Using getContentWindow web client API for passing Context in HTML web resource

We will use the “getContentWindow” client API for retrieving the HTML web resource control and further passing the current form’s execution context in the retrieved HTML web resource.

getContentWindow (Client API reference)

Returns the content window that represents an IFRAME or web resource.

Follow the below steps for passing the form’s context in the form’s HTML web resource.

Step 1 – Create HTML web resource & deploy the same on an entity form

  • Create a HTML web resource which consumes & displays the execution context information.
  • We have create a new HTML web resource which consumes the record’s execution context and displays the current form’s GUID which is given below.
<!-- CRM Crate HTML Code -->
<html><head><meta></head><body style="overflow-wrap: break-word;">
<p>Form Context => </p>
<p id="variable"></p>
<script type="text/javascript">
function setClientApiContext (formContext) {

document.getElementById("variable").innerHTML = "Record GUID Obtained From The Context - " +formContext.entityReference.id;
}
</script>
</body></html>
  • Further deploy or host the newly created HTML web resource on an entity form as shown below.

Step 2 – Create and trigger a new JavaScript web resource

  • We will create a new JavaScript web resource which will use the “getContentWindow” client API for passing the current record context to the above created HTML web resource.
  • Refer the below JavaScript code and deploy it on the form’s “OnLoad” event.
//CRM Crate Function
function SetHTMLContext (executionContext) {
var formContext = executionContext.getFormContext();
let HTMLControl1 = formContext.getControl("WebResource_getContentWindowHTML");

//Asynchornously Waiting For HTML DOM Load
HTMLControl1.getContentWindow().then(

function (contentWindow) {

//Set Client API Context Into HTML Web Resource
contentWindow.setClientApiContext(formContext);
}
)
}
  • For demo purpose, we have triggered the above JavaScript snippet during form’s button click.

Validate the implementation in Dynamics 365 entity form

Once the above HTML web resource and JavaScript web resource are deployed, validate whether the form’s context is getting passed to the HTML web resource as shown below.

Thus, we learned to pass the form’s context in HTML web resource of Dynamics 365 CRM.

3.5 2 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rohan
1 year ago

How to pass the formContext, if opening a html web resource with navidateTo as side bar or popup?

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.