Below is a straightforward approach to merge two Word documents in Dynamics 365—without resorting to third-party paid connectors or extensions. By leveraging a custom Action, a Power Automate flow, and a plugin with OpenXML (embedded via ILRepack), you can seamlessly combine Word files and store the merged result, all in one out-of-the-box solution.
1. Create a Custom Action in Dynamics 365
- In your D365 solution, add a new Action (e.g., new_mergeaction).
- Define two input parameters as string fields,
Document1
andDocument2
. - Define one output parameter as a string field,
MergedDocument
. - This Action will be called by the plugin to perform the merge.
2. Create a Power Automate Flow
- Add Get file content (SharePoint) actions to retrieve both Word documents.
- Insert a Perform an unbound/bound action step (Dataverse connector) targeting the Action from step 1:
- Map
Document1
to@{body('Get_file_content_1')}
.
for example: outputs('Get_file_content')?['body']['$content'] - Map
Document2
to@{body('Get_file_content_2')}
.
- Map
- The Action responds with
MergedDocument
(base64).
3. Develop the Plugin and Register It on the Action
Use a plugin that:
- Accepts
Document1
andDocument2
from InputParameters. - Converts them from base64 (or parses
"$content"
if full JSON). - Merges the docs via OpenXML, adding a page break in between.
- Returns base64 in
MergedDocument
.
Below is a simplified example (text representation):
The key merging logic:
- Open the first doc in a MemoryStream (expandable).
- Insert a page break.
- Use AltChunk to append the second doc.
- Save and return base64.
4. Merge OpenXML with Your Plugin via ILRepack
Because D365 runs plugins in a sandbox, external references like OpenXML must be embedded in your assembly. Use a command like:
"C:\WHSC\packages\ILRepack.2.0.36\tools\ILRepack.exe" /out:C:\WHSC\WordDocumentsMerge\bin\Debug\Merge.dll /keyfile:C:\WHSC\WordDocumentsMerge\WordDocumentsMerge.snk C:\WHSC\WordDocumentsMerge\bin\Debug\WordDocumentsMerge.dll C:\WHSC\WordDocumentsMerge\bin\Debug\DocumentFormat.OpenXml.dll
Then register Merge.dll in the Plugin Registration Tool on your custom Action.
5. Receive and Store the Merged File in Power Automate
- The Action call returns
MergedDocument
in base64. - Add a Create file step (SharePoint) to store the final Word file
Conclusion
With these steps, you can merge two Word documents entirely through standard Microsoft-provided features in Dynamics 365 and Power Automate—no extra costs or third-party extensions required. The combination of a custom Action, a plugin merged with OpenXML, and ILRepack ensures a secure, sandbox-compatible solution that leverages only out-of-the-box functionality.
No comments:
Post a Comment