Tip #1037: Modify data source for Word templates like a boss

Word templates are not the most flexible templating solution for Dynamics 365 but for the simplistic scenarios it does work well, and is quite fast.

Built-in templates are used to be notoriously difficult to modify. Not anymore, thanks to the Document Template Manager from Jim Novak. Now that we can modify the content itself, how about refreshing the data source, e.g. when new fields were added to the entity? Good news is that it’s possible without the need to recreate the template:

  1. Download the template using XrmToolbox
  2. Change file extension from docx to zip. Yes, Word file in the new format is a zip file under the hood.
  3. Open zip using Explorer, and extract the customXml\item1.xml file.
  4. Open item1.xml using your favorite editor.
  5. Insert new fields at the beginning (or wherever) using the format <field_name>field_name</field_name>. In fact, element name can be anything, it’s only used to show the element in the list:
    Xml edit
  6. Add the fields as required:
    Word template
  7. Replace file in the archive (copy in Explorer, open zip file, navigate to customXml folder, paste, say yes to replace).
  8. Rename file back from zip to docx.
  9. Use XrmToolbox to upload the template back, replacing the existing one.
  10. And enjoy the results
    Org document

 

Share on FacebookTweet about this on TwitterShare on Google+

5 thoughts on “Tip #1037: Modify data source for Word templates like a boss

  1. Scott says:

    Editing the XML in the zip file is useful if you want to format date fields, too – I don’t think there’s any way of formatting them otherwise.

    In the Word folder open up document.xml and find your content control. It will have a tag; in there you add fldChar and instrText tags to build a Quote field.

    This is an example that will format the field createdon from CRM with a date format “dd/MM/yyyy”

    QUOTE ”
    createdon
    ” \@ “dd/MM/yyyy” \* MERGEFORMAT

    This creates a new field, writes the QUOTE code, adds ” (watch out that it doesn’t autoformat into “ which won’t work), has the OpenOfficeXML field that CRM will replace with the field value, then closes the ” and finishes the Quote field. When you run the template in CRM it produces a field that looks like
    { QUOTE “30/11/2016” \@ “dd/MM/yyyy” \* MERGEFORMAT }

    I think this would also work with string fields if you wanted to change capitalisation (replace MERGEFORMAT with CAPS to title case it for example).

  2. Scott says:

    Whoops, that ate the XML – here is an encoded version.

    <w:sdtContent>
    <w:r>
    <w:fldChar w:fldCharType="begin"/>
    <w:instrText xml:space="preserve"> QUOTE "</w:instrText>
    <w:t>createdon</w:t>
    <w:instrText xml:space="preserve">" \@ "dd/MM/yyyy" \* MERGEFORMAT </w:instrText>
    <w:fldChar w:fldCharType="end"/>
    </w:r>
    </w:sdtContent>

  3. David says:

    Great tip, thanks!

    Just wondering, will it be possible to insert fields from a grandchild entity with this method?
    For example having a template on opportunity listing every opportunity product for that opportunity. For all opportunity products I want to show the productID coming from the entity Product.

    Any tip on this scenario would be highly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *