INTRODUCTION
Microsoft has put a nice, new front end on their "Portals" technology... and they now call it "Power Pages". This is really a great step forward because it abstracts much of the complexity that underlays the Portals platform. The new UI gives you a "point-and-click" designer with (limited) access to properties of the elements you add to a page. If you are a pro web designer, you'll find it limiting... otherwise, you'll be amazed that you can create a nice web site without having to know much at all about web development (assuming your site doesn't have do much other than support site navigation).
WHAT WE WON'T COVER
It's not the purpose of this article to cover in detail the creation of a RESTful Api endpoint. For the purposes of this article, we created a simple Power Automate triggered by an HTTP Request. This process generates an Endpoint Url which we then used in our XmlHttpRequest.
SOME OF THE NICE STUFF in POWER PAGES
- Templates
- A variety... just to get you up and running
- Page Layout
- Based on Bootstrap and very easy to work with
- Elements
- Every kind of thing you'd want to add to a page... including IFrames, Images, Videos, etc
- Authentication
- If you want restrict access to your site, Power Pages lets you easily implement one of any number of Authentication methods including:
- Azure AD
- Ad Hoc (Forms based)
- Microsoft Account
- Etc
- Or... no Authentication required
- Which makes your site publicly available
- Customization
- Per Page, you can
- add your own JavaScript
- add your own CSS
- customize the HTML
- Per Site, you can
- Associate your site with a custom Domain :)
- Dataverse Integration
- When you have to work with data, seamless integration with the Dataverse makes it pretty simple
When you click the Edit code button, a new browser tab opens with Visual Studio Code for Web and any resources associated with the current page available to edit (html, js, css).
- We need to be able to run a validation process when the value in the Email field changes
- So we need to add an event listener to the Email field
- Fortunately, a Power Page form based on a Dataverse table gives us easy accessibility to each field on the form
- That event listener, when triggered, will pass the Email value to a REST Api endpoint
- This will be done with XmlHttpRequest in JavaScript
- the Endpoint gets a list of any records in our Registration table and passes that back to the Power Page
- The Power Page checks the value returned by the REST Api and responds accordingly
document.getElementById('cr2d2_email').addEventListener(
'change',
email_Change
);
This is reasonably straight-forward. We're hooking a call to a function called "email_Change" when the Email field value changes.Now we need the "email_Change" function. This function will get the value for Email entered by the visitor and then pass that to a function to make the REST Api call.
function email_Change(){
let payload = {};
payload.email = document.getElementById('cr2d2_email').value;
if (payload.email.length === 0){return;}
executeApiRequest(payload,
function(retVal){
if (retVal.length > 0){
document.getElementById('cr2d2_email').value = "";
alert("That email is alreday in use.... please select another.");
}
},
function(error){},
true);
}
function executeApiRequest (parameters, successCallback, errorCallback, async) {
let uri = "https://prod-84.westus.logic.azure.com:443/workflows/xxxxxxxxxxxxxxxxxxxxx11111111abc999/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=uLAnPelNPd2RvVwCbri7j9ctQnwPO7ev54l6yU9UJ2M"; //path to api goes here
let req = new XMLHttpRequest();
req.open("POST", uri, async);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Cache-Control", "no-cache'");
if (async) {
req.onreadystatechange = function () {
if (this.readyState === 4 /* complete */) {
req.onreadystatechange = null;
if (this.status === 200) {
let data = JSON.parse(this.response, dateReviver);
successCallback(data);
}
else {
errorCallback(this.responseText);
}
}
};
parameters ? req.send(JSON.stringify(parameters)) : req.send();
}
else {
parameters ? req.send(JSON.stringify(parameters)) : req.send();
if (req.status === 200 || req.status === 204) {
if (req.status === 200) {
let data = JSON.parse(req.response, dateReviver);
successCallback(data);
}
}
else {
errorCallback(this.responseText);
}
}
}
function dateReviver (key, value) {
if (typeof value === 'string') {
let a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
}
}
return value;
}




No comments:
Post a Comment