A few days ago I wrote a blog post about word/excel templates:
http://www.itaintboring.com/dynamics-crm/wordexcel-templates-rock-in-dynamics/
And got a few interesting responses – it’s great to have readers who can point out something you don’t know.
One of the comments was that we can’t, really, control how related entities in Word Templates are retrieved. Come to think of it, that’s a really important drawback. For example, if I wanted to have a word template for the account entity just so I could see some information on the account itself, and, also, on the open opportunities associated with the account, I would not be able to control which opportunities would show up.
Have a look at the screenshots below. There is an account with 3 open opportunities:
There are, actually, 4 opportunities linked to the account. But one of them is a closed opportunity:
So, I created a very simple Word Template, and here is what I see there:
All 4 opportunities are displayed in the generated document.
Unfortunately, there is no way to define conditions and/or other query parameters (sorting, for example) for the related records when creating a Word Template.
But wait, what if I created a Retrieve Multiple plugin – would I be able to update those queries at “run time”?
As it turned out, Word Templates just ignore RetrieveMultiple plugins all together. Which actually means there are some other interesting implications:
- RetrieveMultiple plugins are sometimes used to implement localization labels – this won’t work
- RetrieveMultiple plugins are sometimes used to implement custom security – this won’t work
- RetrieveMultiple plugins are sometimes used to implement custom filtering – this won’t work
It’s easy to test – you might create a plugin using the code below:
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
throw new InvalidPluginExecutionException(“Test”);
}
All this plugin will do – it will throw an error. So, for example, with this plugin registered in the pre-operation for “RetrieveMultiple” on the Opportunity entity, I can’t load any opportunities view:
Which means I should not be able to create a document using my Word Template above.. And, yet, I can still create that document:
Interesting, isn’t it? Well, all we can do is keep this in mind for now – hopefully, there will be some workaround in the future versions.
PS. I dug a bit more and found an idea on the Dynamics ideas portal which Scott Durow submitted about a year ago, so, if you need this functionality, take a few minutes to upvote there:
https://ideas.dynamics.com/ideas/dynamics-crm/ID0000474
This is one of the things third party apps for document generation have addressed better. In XperiDo for instance there’s a concept of result sets to filter a loop of one-to-many related entity records.
http://www.xperido.com/support/blog/template-design-tipstricks-how-use-resultsets
and of course, Virtual Entities also don’t work with Word Templates :'(