Skip to content

Up to now (pre-version 9.3.3.0), PowerApps Portals and Power Pages only had one method to update entities back to the database, and that was to use Entity Forms.  This has been severely limiting in what can be done with the user interface where updates are needed.  You either had to use Entity Forms or use a clunky workaround of a hidden entity form populated by your own UI.

The good news is that with the recent General Availability of Web API for PowerApps Portals, a whole world of possibilities has opened up. We’re going to look at how we use it.

Group of people around a table during meeting
Diverse colleagues having meeting

What can it do?

The Portal Web API can be used to create, update and delete records. It can also be used to associate and disassociate records – set lookups or many-to-many relationships.

What about read? As of 9.4.1.x this has been made possible and can be done making use of a OData v4 style filtering (with support for a limited set of operators). However previous approaches making use of Liquid and Webtemplates for FetchXml remain possible.

Why use it?

Portal web pages are pretty flexible in what can be achieved; web templates opened up the possibility to react to page parameters, query the database, and send out results, be it HTML, JSON or whatever, based on Liquid logic. But when it comes to writing back, in many instances, actions such as creates, updates and deletes are needed that are not tied with an entity form, and that’s where a Web API comes in.

How do I use it?

There are three essential steps in using Portal Web API.

  • Enable the table (it can only be a data storing entity – accounts, contacts, custom tables are OK but not a system configuration table e.g. reading/writing the adx_ tables). You do this by adding site settings records to enable the table. Add the following site settings for your website:
    1. Webapi//enabled = true
    2. Webapi//fields. Use * for all fields or a comma-separated list of field names
    3. Webapi/error/innererror. Optional: set to true for more errors.
    For example:
power portals blog infograph
  • Set table permissions for the table, allowing permissions to read/write/delete/append/append to. And for the table permission, remember to give them a web role such as Authenticated Users.
  • Write code to use the Web API

Microsoft has helpfully provided a Wrapper AJAX function to making Web API calls easier.   Wrap it in a web template and include it in web templates where you will use the Web API.

For example, get the following code into a web template, call it “WebAPIAJAX”. In your web template where you want to use it, use {%include WebAPIAJAX %}.

PowerApps Portals - coding
PowerApps Portals – coding

Alternatively, put the code in a .js file, upload it as an attachment for a Web File (note: you will need to loosen the upload rules in PowerApps settings to allow the upload of js files, though this can be reverted after the file is imported), and then include the js file in your web template within the script tags such as below.  The web file partial URL would be WebApiAjax.js.

<script src=”/WebApiAjax.js”></script>

 

Once you have the above, the actual using of the WebAPI can be achieved with a call to webapi.safeAjax, for example.

PowerApps Portals - coding
PowerApps Portals – coding

All looks straightforward, but getting the URL and data can be tricky:

1) The URL uses the EntitySet name – plural of your entity name

2) myjson is the JSON object containing the entity details to create, update or delete.

Simple example:

PowerApps Portals - coding
PowerApps Portals – coding

3) In the safeAjax call, use type = POST for creates and associates. It is easy to supply the message for a basic create, but you can also combine the creation of lookup and child records (1:N) and the association all in one message, as follows.

PowerApps Portals - coding
PowerApps Portals – coding

TIP: creating a table but set a lookup to an existing record.  In the above example, if Product X was a record in mycustomentity, and it already existed, then we would use:

PowerApps Portals - coding
PowerApps Portals – coding

TIP: The case of the parameters matter, and normally it is lowercase, but I found that some fields are not all lower case and a field such as “lookupid” needed to be “LookupId”.   How do you find out?  By using the metadata definitions by downloading (a large file) using <my crm>/api/data/v9.1/$metadata and searching for the NavigationPropertyBinding where the name is your lookup field name.

4) Other types:

  • PATCH for updates
  • PUT for updating a single property value
  • DELETE for deleting a record and to disassociates

5) For N:N associations, I have not found it to work with a create and it needed a separate POST call to work. This makes sense as you normally can’t create a N:N relationship until the records have been created.

PowerApps Portals - coding
PowerApps Portals – coding

For the @odata.id it seems to require the full path, so for Portal URI, I used the liquid  “https://{{website.adx_primarydomainname}}.