Dataverse Web Resource as a Template

The need to send emails based on templates is a very common requirement in modern apps.

One of the reasons why you may need templates is to be able to separate the logic of sending those emails from the content of the email itself, so you can list and modify them independently.

Or maybe you want to change something across all your emails, like the signature or the branding colours. If this is the case, you have to first find the flows sending emails, then, update them one by one. A bit cumbersome, isn’t it?

Or maybe you don’t know the contents of the email when you are creating the flow yet.

Power Automate will let you enter the contents of the email directly in the email activity.

If you are using Power Apps, Dataverse or Power Automate to generate emails, what mechanisms do you have available to generate templates with replaceable placeholders easily?

Creativity Spark Power Platform Consulting Company helps you to turn your ideas into brilliant apps, we invite you to contact us and talk about it.

Dataverse Email Templates

Dataverse comes with many tables out of the box, like Contacts, Accounts, Activities, etc. Most of them come from the old CRM days.

Hidden deep into the legacy settings, you could find this old antiquity called Email Templates. But what are those?

Buried deep in the dungeons of legacy settings, there is something called Email Templates.

This is a piece of functionality that still survives from the old CRM days and allows us to create exactly what we need; A place where we can define a Subject and a Body containing placeholders.

Example of an Out of the Box Email Template

These templates can be packed into a solution, so we can deploy them with the rest of the app configuration.

Email templates can be packed into a solution

So far so good? Let’s begin with the inconveniences.

If you want to use these templates, the only way to do it is through an Unbound Action in the Dataverse connector called SendEmailFromTemplate.

You pass to this action the id of the template, which you can get using a simple query, then pass the related record as the Regarding, and the Target will contain the recipient’s information.

Use an unbound action to send emails using a template

The problem is that this email will be created and sent through Dataverse. And to send emails through Dataverse, you need to set up a Server Connection with Exchange, which can be a daunting task.

If you want to send the email using the Outlook activity, this mechanism is not for you.

Is there something else similar to Email Templates available in Dataverse?

Web Resources

Web Resources are virtual files stored in Dataverse that you can retrieve from your Canvas App, Model-Driven app or Flow. You can upload HTML, images, data and other file types as Web Resources in your solutions.

Web Resources are virtual files stored in Dataverse

A template can be seen as just HTML with placeholders. So, why not use Web Resources to store templates?

A template is just HTML with Placeholders, so why not store it as a Web Resource?

Web Resources can be moved across environments using solutions. And in case you need a change, you can update them without having to touch any of your flows or your app.

Step-By-Step

To keep it short, we won’t detail every single step, instead, we will focus on the main ones, and make the solution available for download.

1. Create a text file, and save it anywhere on your computer with the HTML extension.
Feel free to add any HTML content to it. You can use this WYSIWYG editor available online.
For the placeholders, use any you want, and surround it with the % character as shown below. The Flow that we will be creating, will replace these placeholders with the right values as we will see later.

Web Resources do not have a field to set the Subject and the Body of the email as the Email Templates do, so we will use the first line in the file to set the Subject. Immediately afterwards we will be adding a line with ---
This will be the delimiter separating the Subject from the Body.

Create an HTML file and save it anywhere on your PC

2. Create a solution, or open an existing one in the Power App Maker, and add a new Web Resource to it.

Add a new Web Resource to a solution

3. Upload the HTML file just created and complete the fields as shown below.

Add information about the web resource

4. Add a new Manually Triggered Flow, and call it Manual – Replace Template.
The trigger will have two parameters, as shown in the screenshot below.

Add two parameters to the trigger

The first parameter will contain the name of the Web Resource you want to use, and the second will contain the placeholder values in the template.


For example, if the template has two placeholders, document and link, you will be providing a JSON value containing the values like this:

{
"document":"My Document 1",
"link":"https://creativityspark.es"
}

The reason for creating this flow as a Manually triggered is because you will be able to call it from a parent Flow, and reuse it anywhere you need a template.

5. Initialize the variables that will be used in the Flow.

Initialize the values that will be used in the flow

Steps 1 and 2 set the input parameter values to variables with the same name. This is a good practice to make input parameters easier to read in expressions.
In step 2 we convert the value to a JSON object.

Step 3 and 4 are the variables that will contain the outputs of this Flow.

Step 5 is an XML representation that will be used to extract the placeholder names from Placeholder values.

6. Add a List Rows action to get the Web Resource containing the template we want to use.

This step will retrieve the template content

7. Add a Select activity. This step contains the magic expression that returns all the properties in the Placeholder Values. Without entering into too much detail, the expression leverages the xpath function to list all the nodes in the JSON object.

From: xpath(xml(variables('PlaceholderXmlObject')), '/root/*')
Map: xpath(item(), 'name(/*)')

8. The three next three steps extract the contents of the template, separating the Subject and The Body.

Just worth mentioning that Web Resources store their content in Base 64 format, that’s why we use the base64ToString function to convert them.

Step 2 and 3 search for the separator --- line at the beginning and update the Subject and Body variables.

9. In the next part, we iterate through the list of values returned by the Select activity above, and for each placeholder value, we use the replace function in the subject and body.

10. The last step is just returning the Subject and the Body, which at this point contain the values with the placeholders substituted by their intended values.

Respond action to return the subject and the body

How to use it

In order to use this manual flow, we need to call it from a parent. The idea is to embed it anywhere you need a template. The flow below is only a simple example.

We start by creating a manually triggered flow with no parameters, then we call the Replace Template flow passing the name of the web resource, as it appears on its properties in the solution, and the placeholders with their intended values.

When setting the Placeholder Values, you can use Power Automate dynamic values or expressions.

A test Flow that calls the Replace Template

In the last step, send the email, with the Subject and Body returned by the Replace Template flow.

When running this flow, you should receive an email like the one below.

Email received after executing the test Flow

How to edit your templates

We know how to use the templates, but do we edit their content? We offer you three options, from simpler to more complex.

Option 1: Web Resource editor + Notepad

If you open a solution, you will be able to edit the properties of a Web Resource and download and upload the content file from there. You can then use Notepad to edit the contents and upload the new version when you are done. Don’t forget to publish it when you make a change, so it is available in your Flow.

Use the options available in the Web Resources properties to download an upload your templates

Option 2: Xrm Toolbox Web Resource Manager

Webresources Manager has multiple options to let you update and publish your templates easily.

XrmToolbox comes with this very useful Web Resources explorer and editor

Option 3: Use a Canvas App

Creating a Canvas App that updates your templates is a very trivial task. The app shown below is available within the solution containing the flows in this example.

Editing the Template using a Canvas App

See in the video recording, how you can include images in your templates.

Conclusion

We presented above an example of the many uses of Web Resources, and how we can leverage the combination of Power Automate, Canvas Apps and Dataverse to provide a very powerful template system.

This technique can be used not only to generate emails but also to generate and maintain other template-based content, like documents, screen messages, etc.

We hope you enjoyed reading it. If you have any questions, or you need help customizing your Power Platform apps, don’t hesitate to let us know. Feel free to leave any feedback or comments below.

Download the solution with the flow and editor app.

Further Reading