Dynamics 365 Word Templates

Published by Barry Francis on the 09 of November 2018.

Content

Rules/Limitations

Navigation

Enable Developer Tab

Creating a Word Template

Uploading a Word Template

Document Template Security

Workflow using Templates

Microsoft Flow Converting to PDF

Microsoft Dynamics 365 Word Templates

Before Microsoft Dynamics Word templates was introduced, we had to create our own documents through mail merge, custom development or manual capture.

Word and Excel templates have given us the ability to upload templates that can be used to generate a document with dynamic data inserted from Dynamics 365 data sources. This then allows any user the opportunity to create their own custom designed document templates and use them in most scenarios.

Rules/Limitations

There are a few rules/limitations that you need to be aware of before you consider creating your own Word Templates, namely:

  • There is a limit of 100 associated records that can be returned for each relationship.
  • The report cannot filter/sort the associated data that is returned
  • If any of the attribute values are empty, the placeholder reserves its location, meaning if no value is passed through, the attribute location will remain blank.
  • To list records as seen in a view, you will need to use an Excel template.
  • You will not be able export a template from a Sandbox instance and import it into another Dynamic 365 instance. The only possible way to do this is to ensure that the most recent Word Template that was uploaded to Sandbox is uploaded to Production.
  • Inserting a content control other than Picture or Plain Text can cause Word to freeze. To prevent this:
    1. Only add fields as Plain Text or Picture
    2. Do not make any textual changes to the added content control

Reference (https://docs.microsoft.com/en-us/dynamics365/customer-engagement/admin/using-word-templates-dynamics-365)

  • There is no functionality that allows for an update of an existing document. Any updated document will need to be manually uploaded and the previous document deactivated or deleted.

To begin creating a Microsoft Word Template, navigate to Settings > Templates > Document Templates.

Then click on the New button to begin creating a Dynamics 365 Word Template.

Enable Developer Tab

This tab is not enabled by default and must be enabled before you can begin creating new word templates.

To enable this tab, select File > Options > Customize Ribbon > Main Tab within Microsoft Word and then tick the Developer tab.

Creating a Word Template

When ready, click on the New button and then the Select Entity button.

There you will be required to select all the entities you wish to include against the primary entity you’ve selected.

Each associated entity in the relationship list will allow for additional mappings to be included.

NOTE

  • Try not selecting all the entities in the lists as the template will take an exceptional amount of time to be downloaded.
  • From the time on writing this, I experienced some issues with the downloading of the templates from Internet Explorer, sometimes the browser would stall and cause my CPU to run at 100%. I found that using Chrome or Firefox was much quicker.
  • If you create a Word Template from the settings module, the document template will be considered a system template and will be visible and accessible by all who have the correct security permissions. If you create a new word template from an entity record, it will be created as a personal document template! This document will not be visible from within the setting module, but via advanced find under Personal Document Templates.

A blank word document will be opened.

Click on the Developer tab and then click on the XML Mapping Pane button under it.

On the right XML Mapping pane, click on the drop down list and choose the XML part labelled: urn:Microsoft-crm/document-template/[ENTITY]

This will load all the attributes that fall under the primary entity you selected.

The 1:N relationship will show under the main entity:

To begin, build a template that you can simply add Dynamics 365 data fields to:

Then, using the XML Pane, right click on any of the fields and choose to input them as plain text.

This will serve as a mapping for entity record you run this template from.

Should you wish to create a table of associated records:

  • Highlight the initial row you wish to have repeated
  • Right click the associated record and choose to insert content control that is Repeating

  • Add the mappings thereafter as you did for the rest of the template (Plain Text Control).

The document must be saved to a location you can reference when choosing to upload a word template.

Uploading a Word Template

To upload a document, first know where it is stored.

Then navigate to document templates in Dynamics 365 and choose to Upload a Document.

Drag and drop the new template into the window and choose to Upload. Once uploaded, Dynamics will open a window that showcases the document record.

Keep in mind that you cannot update existing documents, so any existing document that needs to be updated, must be uploaded as a new document and all references updated to look at this new document.

Navigate to the entity you set as the primary entity for the document template and choose to open it as a word template.

After the template has opened, all the mappings should be set correctly, and all members of your instance can access these templates.

Document Template Security

The document template entity is controlled by two sets or security permissions. This can be viewed by opening the Business Management tab within the security role.

There you can determine who can maintain, create and view document templates.

Also note that you can determine if users have the permission to create their own personal document templates.

Keep in mind that the Personal Document Templates are only accessible via Advanced Find and do not show under the document template entity in the settings module.

Any documents created as a personal document template will not be visible to any other users and will be treated like a personal view or chart.

Workflow using Templates

Should you require it, you can use workflow to send out your templates automatically. Please keep in mind that the entity must have Note enabled under its entity settings for this to work.

To do this, create a new workflow with its own sets of triggers and conditions.

Then, add an action step under the condition/step that uses SetWordTemplate.

When setting the properties, set the template you’ve imported and the target GUID it will use when retrieving the data.

This will create a note with the template attached as an attachment

The only issue with this approach is that users will have access to a Word Document that they can update should they want too. Use the flow conversion below to convert the document to a PDF to prevent this.

Microsoft Flow Converting to PDF

In this scenario, I’l only going to walk through the steps of setting up a conversion to PDF flow and not work through what each of the steps mean and do. For more information on Microsoft Flow, check out the blog post link.

Here is how you can setup a flow to convert a word template to PDF.

  • Start by running from a create or update trigger, specifying that it should trigger from when a note is created.

Then add a condition under the trigger to ensure that the flow only runs against notes that have a word document attachment.

To do this, add the following in Advanced Mode:

@equals(and(equals(triggerBody()?[‘isdocument’], true), and(equals(‘quote’, triggerBody()?[‘objecttypecode’]), endsWith(coalesce(triggerBody()?[‘filename’], ‘NULL’), ‘.docx’))), true)

This ensures the rest of the flow only runs against notes where there is an attachment (is document = true) and where that attachments filename ends with .docx.

  • Under the yes path, get the parent record the note is connected too by setting a Get Record.

  • Then, create a new file in OneDrive with the document from the notes trigger and follow that up with a convert file action, passing through the previous OneDrive record id and setting the target type to be PDF.

  • Set another Dynamics 365 action that creates a new note record with the document section having the above converted file. Make sure to set the file name as something outside of the .docx to ensure another flow is not triggered to convert the same attachment.

  • To prevent users from having any access to the original word template, you can use the same flow to delete the original note.

  • Finally, make sure you clear out the OneDrive file you created in the flow.

The final result, when a new note is created, flow will take the attachment and recreate a new note, but with a PDF version of the word template, preventing users from updating the word document.

You can also set the flow to get the owners record, granting you access to their email address. That way, you can set up an email within the flow to email them the attachment as well.

2018-11-12T13:35:18+00:00