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

 

13 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).

    • Very nicely done, Scott. Would you like to extend it to a full tip (just email it to jar@crmtipoftheday.com)? Perhaps any other magic that is available? I did some clumsy workarounds to achieve some conditional formatting, and it looks like direct xml editing might be the way to improve it.

      • Antonio says:

        Has anybody used this fix? I get an error saying that Word cannot open the document if I apply it.

    • Pramod M says:

      Hi Scott,

      Here is my sdtcontent. When ever I am changing this and opening the word document it says invalid, Could you please help me in this regard?

      fam_receiveddate

      I tried

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

  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>

    • Rafael says:

      Hi Scott,

      I tried using this approach but seems like it’s no longer possible.

      1. after adding the additional tags for creating the quote, when trying to open the document it throws an error for unreadable parts (gets rid of the added thing)
      2. If directly uploaded and used from dynamics 365.. the field comes completely empty.

      Have you tried this recently ? or have you found another approach instead?

      The only think I could think of right now is more and more customization which is not a cool thing in terms of maintenance.

  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.

  4. Henrik says:

    Maybe this tip is useful too here:

    I made a word template on Opportunity. Months later I added a new field on opportunity, but wanted to add this in the word template also. I did this instead of the xml thing above:

    1) using XRMtoolbox (Document Template Manager from Futurez) downloaded the old version of the template
    2) downloaded a blank new word template from CRM – this one includes the new field(s)
    3) in 1) you ctrl+a everything and copy it to the new 2) template
    4) then you have the new field and can insert it in the template and upload it to crm

  5. Carloss says:

    Can I add a relationship to another table in a similar way?

  6. Dina says:

    What do you search for in XrmTools?

Leave a Reply

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