问题
Is there an event that a plugin can be registered on when a FetchXML (or even SQL) report is run?
RetrieveMultiple and Retrieve do not get fired!
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// The FetchXML report does not fire the plugin on RetrieveMultiple
if (context.InputParameters["Query"] is FetchExpression)
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
using (Context linq = new Context(service))
{
// Do the work.
}
}
}
回答1:
I had the same issue and the solution is the following:
Reports do not trigger plugins. SQL reports bypass CRM completely, and Fetch reports also bypass the plugin pipeline
More about this here: https://crmbusiness.wordpress.com/2014/11/25/fetchxml-reports-do-not-trigger-retrievemultiple-plugins-in-crm-2011/
回答2:
I have had similar needs to perform an action when a report is run and the best [supported] way I could find to accomplish this by triggering the report to run via Javascript.
The rough process is this:
- In the applicable entity, create ribbon button to launch the report via Javascript. (This post gives a how-to: Trigger a report from a ribbon button)
- As part of Javascript function which runs the report, update a field (e.g. "new_ReportRunOn") in the respective record (A standard REST update operation would work here) prior to report run code. The idea here is simply running the report will always cause the "new_ReportRunOn" field to update first.
- Have your plugin listen for changes on this field by applying a filter only the "new_ReportRunOn" field. (Alternately, you could just have your logic run in Javascript [as part step 2] instead of updating a field just to trigger a plugin.)
This isn't without drawbacks though:
- You would need to "hide" the report in CRM Reports section so it is essentially only accessible via the entity form. It would still be visible in the Reports section if users chose to browse "All Reports" in the reports section. If a "savy" user launched the report this way then it would bypass your Javascript code which fires the plugin.
- If the report can be run across multiple records (e.g. the user selects applicable records from the entity grid view) then additional considerations/coding would need to be done. It is possible to handle this case, but based on your question it doesn't appear this would be use case.
I've had process similar to this in place for a while on our 2013 install and it actually works quite well. It isn't ideal but gets the job done.
来源:https://stackoverflow.com/questions/24866176/dynamics-crm-fetchxml-report-event-for-plugin-to-fire-on