Hello everyone,
Today, I had an interesting challenge from one of my client.
Here is the use case :
"We want to keep using the Word template provided by the Dataverse because it's easy to configure the data mapping between the Tables and the word template. But we also want to integrate values from another system", and the solution must be as integrated as it is today for the enduser.
Basically, the challenge here is to "recreate" the selection screen of a template and use a Power Automate flow to make all the job.
In details this will give something like :
- Create a custom page opened by a click in the ribbon (here is how to do it : https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/navigate-to-custom-page-examples?WT.mc_id=BA-MVP-5003014)
- Retrieve all templates (word is the example) related to the selected record's table
- Select the whished Template
- Generate the document via Power Automate
We will focus on the step 4 as this is the most fun part 🙂.
Prerequiste :
In order to make this work, you need upfront to add your extra tags in the word template which will be uploaded to the Dataverse.
Detailed Schema of step 4 :
Step 2) I decided to use the HTTP with Microsoft Entra ID (preauthorized) connector to ease the POC. Of course for the step, you could go with a Custom connector on top of the Dataverse API. Reason for not using the standard Dataverse connector is that this action is not "exposed".
This will give you something like :
URL : /api/data/v9.2/ExportWordDocument
BODY :
{
"EntityTypeCode": 2
},
"SelectedTemplate": {
"@odata.type": "Microsoft.Dynamics.CRM.documenttemplate",
"documenttemplateid": "GUID HERE"
},
"SelectedRecords": "[\"GUID HERE\"]"
}
Parameters :
- You can find all EntityTypeCode here.
- The SelectedTemplate corresponding to the Document Template ID selected.
- The SelectedRecords corresponding to an array of the records you want to generate the word document for.
Step 3) Using a compose will allow you to prepare the file content we will use to create the document on SharePoint in the next step :
{
"$content-type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"$content": @{body('ExportWordDocument_Action')?['WordFile']}
}
Here we define the content type for a word file and keeping the property "WordFile" only from the ExportWordDocument action.
Step 6) We now have our document generated from the dataverse in SharePoint, we can now fill it with the external data we retrieved from the previous step :
On the above screenshot, we can see the placeholder which are used by the Dataverse word template generation, our focus is about the ones in the red rectangle which will be used for our extra data.
All you need to do here is to map those fields with the data you extracted.
Step 7)
Here the File Content property will be filled with the body of the previous action, ie @{body('Populate_a_Microsoft_Word_template')}
.
Wrap up :
We created a dedicated process to keep using the word template generation from Dataverse to ease the data mapping for the end user update.
We extended it with external data and put the final file on SharePoint (it could be send by email, send back the file content to the Custom Page to directly download it or whatever way you want 😋).
Hope this can help you some times.
Enjoy,
Cheers
PS - Possible improvement on this POC :
- Use a Service Principal with the Entra ID connector otherwise your users will be lost or use a Custom Connector on the top of the Dataverse API or use a provided connection with the "Run only user" section of your flow.