I recently encountered a question regarding the possibility of previewing a PDF file inside Dynamics 365 CRM or a Model Driven App without opening an additional tab or window. After conducting some research, I found that there is no out-of-the-box (OOTB) feature for previewing PDF files. However, I devised an approach to achieve this by leveraging client-side scripting, specifically using JScript.
One of the methods I explored is the Xrm.Navigation.openFile(file, openFileOptions) script. This script allows you to open a file in another tab or window. However, the goal is to have the file open inline within the Unified Client Interface (UCI) page. More details on this script can be found in the Microsoft Docs.
To implement this, I created a file field on the Accounts Entity named "Test File" and added it to the main form. The preview feature is intended for files uploaded to this column.
To accomplish the preview functionality, I decided to create an HTML web resource and a JS web resource. The HTML web page serves as a canvas for previewing a PDF file inside it, while the JS web resource is designed to open a modal window inline, containing the web page for previewing the file.
The below is the HTML code
And the below is the JScript code
To call up the preview modal, I created a command button for the main form using the command editor and added the JavaScript web resource as an action. To render PDF files, I've integrated the PDF.js library. Alternatively, users can utilize their browser's built-in PDF viewer for this purpose. Subsequently, I saved and published the changes.
Following these steps, go to the Accounts main form, upload any PDF file to the "Test File" field, and then click on the "Preview File" button in the Ribbon. This action should display the file opened inside the page.To demonstrate the implementation, I've attached a gif for your reference.
Hello as i was goin through MS docs
ReplyDelete"../WebResources/ClientGlobalContext.js.aspx" type="text/javascript"
using this line was not suggested, what are your views on that
But if you want to have the form context inside the html you need to use the line
DeleteHi,Thanks for the great job and sharing. As a newbe in Dynamics CRM environment can you share also where I should stor / upload the HTM webresource? Thanks in advance.
ReplyDeleteHi, thank you! I just wanted to confirm if this works only for PDF files.
ReplyDeleteYes, It will only work for pdf files
DeleteI mustbe doing sum wrong ,, cause this aint working for me dude :(
ReplyDeletePlease check the web resource and logical names. Let me know if you are still facing the issue
Delete