Skip to content
Franco Mussopower pages
Room with purple lighting and white wall
Photograph · Juliana Tanchak / Unsplash
Guide No.025
TutorialPortals Web API

Authenticate with Power Pages’ Portal Web API using the Wrapper AJAX function

If you’re a regular visitor here, you may have noticed I cover a lot on the Portal Web API.

Level
03CustomiseStage 3 of 4
Read
3 min
Includes
2 snippets · 1 figure
Written
Jan 2023

Introduction

If you’re a regular visitor here, you may have noticed I cover a lot on the Portal Web API.
It’s a passion of mine for many reasons but especially because it allows us to break free of the standard list and form UI.

Before we ever send an API call, we need to authenticate with the API and prove this is coming from a Power Pages site / Power Apps portal. Don’t worry, this is as simple as copying and pasting a code snippet from Microsoft Docs Microsoft Learn.

We use the same snippet of code every time so I prefer to have this saved in a web template for easy re-use (following Nick Doelman’s excellent advice).

Step 1 – Create a web template containing the function, for easy re-use

Open the Portal Management app
Select Web Templates from the Content section in the left navigation
Click New
Select the required website
Give your web template a name. I use Portal Web API Wrapper

In the Source field, type opening and closing script tags

Copy the wrapper function from https://learn.microsoft.com/en-us/power-pages/configure/write-update-delete-operations#wrapper-ajax-function

Power Pages
 Create a Web Template for the Portal Web API Wrapper
Fig. 01 Create a Web Template for the Portal Web API Wrapper

Paste the function between your opening and closing script tags
Save your web template

Step 2 – Include (use) your new wrapper function within a web template

Wherever we want to make use of the portal web API, all we need to do to authenticate is to add the link below (before any calls to the API):

Liquid
{% include 'Portal Web API Wrapper' %}

For convenience, here’s a copy of the web template, ready to copy and re-use:

Portal Web API Wrapper AJAXHTML
<script>
(function(webapi, $){
    function safeAjax(ajaxOptions) {
        var deferredAjax = $.Deferred();
        shell.getTokenDeferred().done(function (token) {
            // add headers for AJAX
            if (!ajaxOptions.headers) {
                $.extend(ajaxOptions, {
                    headers: {
                        "__RequestVerificationToken": token
                    }
                });
            } else {
                ajaxOptions.headers["__RequestVerificationToken"] = token;
            }
            $.ajax(ajaxOptions)
                .done(function(data, textStatus, jqXHR) {
                    validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
                }).fail(deferredAjax.reject); //AJAX
        }).fail(function () {
            deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
        });
        return deferredAjax.promise();
    }
    webapi.safeAjax = safeAjax;
})(window.webapi = window.webapi || {}, jQuery)
</script>
Go deeper · Power Pages Mastery

This guide is one technique. The course is the method.

The course covers this in Level 3 · Advanced customisation: “Portal Web API — deep inserts, editable grids”, alongside “AJAX and asynchronous JavaScript”. A structured programme distilled from dozens of real portal launches — from messy stakeholder requirements to successful go-live.

  • 6 weeks
  • 3 levels
  • Live training + video lessons
Where this fits
  1. Level 1Foundations
  2. Level 2Extending the platform
  3. Level 3Advanced customisation
    • Portal Web API — deep inserts, editable grids
    • AJAX and asynchronous JavaScript
Franco’s engaging teaching style made training a pivotal contribution to my professional growth. The walkthrough of a Power Pages site resembling Spotify left an indelible mark.
Bhargav Reddy — Solution Architect — TCS
Filed underPortals Web API

Comments

No comments yet. Spotted a gotcha I missed? Say so below.

Leave a comment

Your email address won’t be published. Comments are moderated.

Consulting

Need a Power Pages build that editors love and brands survive?

I help teams design and build Power Pages sites with bespoke UX. Tell me about yours.

Search

Book a discovery call

30 minutes. Bring your trickiest Power Pages problem — I’ll tell you honestly whether I’m the right fit.