As you probably already know, there’s no built-in way to specify the filename of a report you want to export to PDF, Excel, Word, etc. Then a colleague pointed me to this article.
First off, the code for the below solution can be downloaded here.
This one took a while to figure out so I hope this saves people a few hours of tinkering. As you probably already know, there’s no built-in way to specify the filename of a report you want to export to PDF, Excel, Word, etc. At first I thought of attempting to modify the
We decided to try out the code, so we added it as a web resource and placed the
setHandler function in the
onload event of the Quote form. Testing it out, it worked without a hitch, however, I wasn’t too fond of using
onreadystatechange, etc.). After several hours, I finally understood why Alex (who wrote the above article) used
setTimeout in his code:
viewer.aspxloads, it first loads a blank HTML file (
/_static/blank.htm) into an
iframeusing an object called
- When you export a report, a URL is dynamically generated based on the
ExportUrlBaseproperty of the
The key here is to modify the
ExportUrlBase property because that is what contains the
FileName URL parameter. You can inspect
ExportUrlBase in the report window using IE8’s Developer Tools (hit F12):
Which should result to something similar the following:
Given the above, attempting to set the
ExportUrlBase when the
onload for the
iframe will fire, but remember that the source for the iframe is a blank document! Unfortunately, the best way to tackle this is to use timing to ensure that the
reportViewer object finally gets loaded. So, in the end, I did end up using a timing function but in this case, I used
setInterval instead of a recursive
setTimeout; in addition, I also utilized
regex to change the
FileName value, both of which resulted in a simpler approach.
To use the script below, create a new Web Resource in CRM and set the
onload event for the form to
setReportFilename. For convenience, you can download the script here.