how to get the XRM/FromContext on html page in D365.

Let’s image that you created a simple HTML web resource that needs to do some work and then pass it back to the form. Previously to do so you would use parent.Xrm. However, the problem is that previous parent.Xrm and Xrm.Page is deprecated. But know you have supported way to do so - welcome getContentWindow.

syntax: 
formContext.getControl(arg).getContentWindow().then(successCallback, errorCallback);

1. Create a java script webresource in Solution with below code.
2. Add the Event onLoad / OnChange on Entity Form and Pass Parameter.

function Form_OnchangeOROnLoad(executionContext) {
    var formContext = executionContext.getFormContext();
    var wrControl = formContext.getControl("new_WebResourceName.htm");
    if (wrControl) {
        wrControl.getContentWindow().then(
            function (contentWindow) {
                contentWindow.setClientApiContext(Xrm, formContext);
            }
        )
    }
}

3. Create Html WebResource in Solution wit below Code.
4. Add Html file on Grid or Field on Form.

The following example shows how you can use this method with a HTML Web resource(new_myWebResource.htm).

function setClientApiContext(xrm, formContext) {
    // Optionally set Xrm and formContext as global variables on the page.
    window.Xrm = xrm;
    window._formContext = formContext;
    OnloadFunction();
    // Add script logic here that uses xrm or the formContext.
}

Reference: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/getcontentwindow

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#