dynamics-business-centraldynamics-al

Is it possible to render an external URL via a control add-in?


I am working on a punchout application for Business Central and I would like to redirect a user to external URL via a control add in. Right now I have a startup script

Microsoft.Dynamics.NAV.InvokeExtensibilityMethod('ControlReady', []);
console.log("Add-in initialized");

an "openUrl" script

(function () {
    console.log("Add-in started");
    window.PunchoutCatalogAddIn = {
        OpenUrl: function (url) {
            window.open(url, '_blank');
        }
    };
})();

the control add in object

controladdin PunchoutCatalogAddIn
{
    StartupScript = './scripts/startup.js';
    Scripts = './scripts/openurl.js';

    RequestedHeight = 700;
    MinimumHeight = 700;
    MaximumHeight = 700;

    RequestedWidth = 700;
    MinimumWidth = 700;
    MaximumWidth = 700;

    VerticalShrink = true;
    HorizontalShrink = true;
    VerticalStretch = true;
    HorizontalStretch = true;
    event ControlReady();
    procedure OpenUrl(url: Text);
}

and the page the user interacts with

page 50114 PunchoutCatalog
{
    PageType = Card;
    ApplicationArea = All;
    Caption = 'Open External URL';

    layout
    {
        area(content)
        {
            usercontrol(PunchoutCatalogAddIn; PunchoutCatalogAddIn)
            {
                trigger ControlReady()
                begin
                    Message('PunchoutCatalogAddIn control is ready.');
                end;
            }
        }
    }

    actions
    {
        area(processing)
        {
            action(OpenCatalog)
            {
                Caption = 'Open Catalog';
                trigger OnAction()
                begin
                    Message('Attempting to open URL: %1', ResponseUrl);
                    if ResponseUrl = '' then
                        Error('Response URL is empty.');

                    CurrPage.PunchoutCatalogAddIn.OpenUrl(ResponseUrl);
                end;
            }
        }
    }

    var
        ResponseUrl: Text;

    procedure SetResponseUrl(Url: Text)
    begin
        ResponseUrl := Url;
        Message('Response URL set to: %1', Url);
    end;
}

The strange thing is that, although the openUrl script is run (it console logs "add-in started"), it does not appear in the DOM when I go to inspect the browser, and nothing renders in the iframe. Could someone point me in the right direction? Thank you in advance!


Solution

  • If all you want to achieve with the Control Add-in is to open an external URL, you should use the built-in WebPageViewer.

    You just add it to the page as a usercontrol:

    usercontrol(PunchoutCatalog; WebPageViewer)
    {
        ApplicationArea = All;
    
        trigger ControlAddInReady(CallbackUrl: Text)
        begin
            CurrPage.PunchoutCatalog.Navigate(ResponseUrl);
        end;
    }
    

    You don't have to call CurrPage.PunchoutCatalog.Navigate() from the ControlAddInReady trigger.
    It is available from anywhere on the page you added the usercontrol to.