Skip to content

Kunter-Bunt/PowerPagesActions

Repository files navigation

PowerPagesCustomAPI

Call custom code from Power Pages! This page creates a mapping from a Custom API to the Power Pages OData Endpoint, allowing you to call selected Custom APIs from your website code.

Video Guide

Setup Guide

Setup

  1. Install Managed Solution
  2. Create Table Permission on mwo_powerpagesaction, Global Access, Read. Select the appropriate WebRoles, for more information regarding Security, see below.
  3. Create 2 Site Settings
    • Webapi/mwo_powerpagesaction/enabled : True
    • Webapi/mwo_powerpagesaction/fields : *
  4. Create an Action Configuration (mwo_powerpagesactionconfiguration) with an operation name that you will pass from Power Pages and the unique name of the Custom API you want to call. You will find Action Configuration in the Power Pages Management App in the section Actions.
  5. Create Webfile actions.js, include the content from actions.js in this repository and then include it on your page html <script src="/actions.js"></script>.
  6. Call callAction(operation, inputs, onSuccess, onError) where operation is the operation name you assigned to the Action Configuration and inputs is an object with the Request Parameters of the Custom API. onSuccess and onError are functions receiving the mwo_outputs as a parsed object.
    • Success Responses contain the Response Properties of the Custom API in the object.
    • Error Responses from the Custom API/Adapter will have a property message.
    • Error Responses from the Power Page OData API will be passed raw (unless it's a known configuration error) and are likely to have an error object with a message property.
    • It is advised to check var errorMessage = outputs.message || outputs.error?.message; for most common errors in the onError function.

Security

TODO

Sample Code

The sample code calls a Custom API (adc_ValidateVAT) with a Request Parameter VAT and three Response Properties Valid, Name and Address. It is assumed that the Action Configuration has set "ValidateVAT" as the Operation. In this sample, a missing Site Setting, Table Permission or Action Configuration will be reported to the html element with the id "valid".

$(() => {
    $("#vat").change(() => {
        var inputs = {};
        if ($("#vat").val())
            inputs.VAT = $("#vat").val();

        actions.callAction("ValidateVAT", inputs, onSuccess, onError)
    });

    function onSuccess(outputs) {
        $("#valid").text(`Valid? ${outputs.Valid}`);
        $("#name").text(`Name: ${outputs.Name}`);
        $("#address").text(`Address: ${outputs.Address}`);
    }

    function onError(outputs) {
        $("#valid").text(`Error: ${outputs.message || outputs.error?.message}`);
        $("#name").text("");
        $("#address").text("");
    }
});

About

Sample to call custom code from Power Pages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages