Skip to main content
 

PowerApps Portals – Use Web API to Create, Update and Delete records

*

Hitachi Solutions > Blog > 2021 > 04 > PowerApps Portals – Use Web API to Create, Update and Delete records

Up to now (pre-version 9.3.3.0), PowerApps Portals 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 (https://docs.microsoft.com/en-us/powerapps/maker/portals/web-api-perform-operations) of Web API for PowerApps Portals, a whole world of possibilities has opened up.   We’re going to look at how we use it.

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?  Well, that is already achievable using web templates and fetchxml, so there was really no need for the Portal Web API to provide this as well.

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 entity (it can only be a data storing entity – accounts, contacts, custom entities are OK but not a system configuration entity).   You do this by adding site settings records to enable the entity.  Add the following site settings for your website:
    1. Webapi/<logical entity name>/enabled = true
    2. Webapi/<logical entity name>/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:

  • Set entity permissions for the entity, allowing permissions to read/write/delete/append/append to. And for the entity 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 (from Microsoft docs: https://docs.microsoft.com/en-gb/powerapps/maker/portals/web-api-perform-operations ) into a web template, call it “WebAPIAJAX”.  In your web template where you want to use it, use {%include WebAPIAJAX %}.

Alternatively, put the code in a .js file, upload it as an attachment for a Web File (will need to loosen the upload rules in PowerApps settings to allow the upload of js files), 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.

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:

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.

 

TIP: creating an entity 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:

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.

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

 

Speak to our experts today

 

References:
https://docs.microsoft.com/en-gb/powerapps/maker/portals/web-api-overview
https://docs.microsoft.com/en-gb/powerapps/maker/portals/web-api-perform-operations
https://docs.microsoft.com/en-gb/powerapps/maker/portals/web-api-http-requests-handle-errors

 

*

Tom Kan

Tom Kan is a Solution Architect at Hitachi Solutions Europe who specialises in Dynamics CE, Power Platform and Portals.

*

Author Spotlight

Tom Kan

Tom Kan is a Solution Architect at Hitachi Solutions Europe who specialises in Dynamics CE, Power Platform and Portals.

Recaptcha Failed
Sorry, something went wrong trying to submit your data

Thank-you for contacting Hitachi Solutions. A member of our team will be in contact shortly.

Icon