Move from using Xrm.Page to formContex in UCI form based PowerApps WebResources

The usage of Xrm.Page is currently deprecated, still available due to it’s high usage, but still deprecated. When will it go away, we don’t really know yet. How then can we use it’s formContext replacement within a web resource. Well Microsoft recently added a the getContentWindow() function to the context which allows us to get the actual content of a web resource. By adding an onload function to our form and utilizing getContentWindow() we can now call javascript on our web resource and pass it the formContext. Additionally you can add the context to the window object of the web resource so that the formContext can be used throughout the lifecycle of the web resource.

Here is a quick tutorial on how to pass the formContext to a WebResource on a UCI Form.
formContext in WebResource
Canvas App Video

Form OnLoad Code

// This should be in a script loaded on the form. 
// form_onload is a handler for the form onload event.
function form_onload(executionContext) {
    var formContext = executionContext.getFormContext();
    var wrControl = formContext.getControl("WebResource_getCurrentFormId");    
    if (wrControl) {
        wrControl.getContentWindow().then(
            function (contentWindow) {
                var contentAvailable = self.setInterval(function () {
                    //we need to check that the content window was actually loaded
                    // and that our function is available to call on the web resource.
                    if (contentWindow.setClientApiContext) {
                        clearInterval(contentAvailable);                  
                        contentWindow.setClientApiContext(formContext);                                               
                    }                    
                }, 100);                
            }
        )
    }
}

WebResource Code

<!DOCTYPE html>
<html>
    <head>
        <script>
            // This script should be in the HTML web resource.
            // No usage of Xrm or formContext should happen until this method is called.
            function setClientApiContext(formContext) {
                // Optionally set the formContext as global variables on the page.
                window._formContext = formContext;
                
                // Add script logic here that uses xrm or the formContext.
                // For this test we will just get the current form id
                document.getElementById('formIfo').innerHTML =_formContext.ui.formSelector.getCurrentItem().getId();
                document.body.style.display = "block";
            }
        </script>    
    </head>
    <body style="display: none;">
        Current Form Id: <span id="formIfo"></span> 
    </body>
</html>

Comments

  1. Hello, I would like to create a "Batch Confirm" invoice button that is located on my Invoice home grib. I am currently able to navigate to all the Invoice forms that were selected using Xrm.Navigation.navigateTo but I am unable to get the formContext. I need to get the form context in order to use the OOTB invoice confirm function that requires formContext. Any ideas?

    ReplyDelete

Post a Comment

Popular posts from this blog

Add User As Local Administrator On Domain Controller

Calling Dataverse Web API in PowerShell using Client Credentials

Windows Server 2008R2 VMs Shut Down After 1 to 2 Hours