Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamics CRM 365 : Downloading a Word Document Template via a Button on the Ribbon

Currently users have to click the ellipses, word templates, and finally quote to download the word template.

ellipses, word templates, and finally quote

To make it easier for our users we would like to have the document download when pressing the "print quote" button on the ribbon.

print quote

Is this possible? If so how would I go about doing this? I understand how to edit the ribbon using the ribbon workbench. I need to know how to download a word template using the ribbon. ribbon workbench

If the solution is using the ribbon workbench, what command can I enter to get the word template to download?

like image 697
MasterProgrammer200 Avatar asked Jun 13 '17 15:06

MasterProgrammer200


People also ask

How do I download a template from Dynamics 365?

Select an environment and go to Settings > Data management > Templates. In the Templates for Data Import dialog box, choose the record type that you want to download the template for, and then select Download. In the file download box, select Save or Save as and navigate to a location for the file. Select Close.


4 Answers

When you click the templates flyout, it's dynamically populated through an invocation of /AppWebServices/DocumentTemplate.asmx, which returns the XML for the menu.

The flyout for Word Templates in the Incident home page grid looks like this:

<Menu Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu">
    <MenuSection Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.CreateTemplates" Title="Create Word Template" Sequence="10" DisplayMode="Menu16">
        <Controls Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.CreateTemplates.Controls">
            <Button Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.CreateTemplates.Controls.00000000-0000-0000-0000-000000000000" Command="incident|NoRelationship|HomePageGrid|Mscrm.WordTemplate.CreateWordTemplate.Grid" Sequence="10" ToolTipDescription="Create Word Template" Alt="Create Word Template" LabelText="Create Word Template" />
        </Controls>
    </MenuSection>
    <MenuSection Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.WordTemplates" Title="Word Templates" Sequence="20" DisplayMode="Menu16">
        <Controls Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.WordTemplates.Controls">
            <Button Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.WordTemplates.Controls.9b77c5b0-1033-4741-a01c-afdbdb1c3f22" Command="incident|NoRelationship|HomePageGrid|Mscrm.WordTemplate.TemplatesMenu.Grid" Sequence="10" ToolTipDescription="Case Summary" Alt="Case Summary" LabelText="Case Summary" />
        </Controls>
    </MenuSection>
</Menu>

I don't have the means to try it out at the moment, but I'd try and "copy" the last <Button>:

<Button Id="incident|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.incident.WordTemplates.Menu.WordTemplates.Controls.9b77c5b0-1033-4741-a01c-afdbdb1c3f22" Command="incident|NoRelationship|HomePageGrid|Mscrm.WordTemplate.TemplatesMenu.Grid" Sequence="10" ToolTipDescription="Case Summary" Alt="Case Summary" LabelText="Case Summary" />
like image 120
Alex Avatar answered Oct 07 '22 04:10

Alex


It's possible to do this using only supported features of CRM (of course I'm sure it's also possible to do using unsupported javascript, but I don't have time currently to investigate this). The steps that you should take to achieve the functionality you want:

  1. Create new process of type Action, bound to entity that you want to create a template for (the reason why I suggest Action here, is that it can be easily invoked using JavaScript and CRM WebAPI)
  2. In this Action add single step - invoke an Action and choose built-in action "SetWordTemplate"
  3. Set Properties of this action - choose the template that you need and dynamically set the target to current entity (using Dynamic Values assistant) If you never used this action - it simply creates a given word template and adds it as an annotation to your entity
  4. Now you need to write logic inside your button (I'm assuming you know how to add a button using Ribbon Workbench or whatever)
  5. Call your action using WebAPI
  6. Find annotation that was just created for your entity with the attached document
  7. Download the attachment (you can show some prompt for the user or simply force the download the file, user will have to save it)
  8. Delete the annotation

Maybe not a one-liner, but keeps you in the supported zone...

like image 25
Pawel Gradecki Avatar answered Oct 07 '22 05:10

Pawel Gradecki


ExecuteWordMerge = function (wordtemplateid, entitytypecodeint, ids, templatetype, fieldforfilename, filenameoverride) {
        try {
            Xrm.Page.ui.clearFormNotification("worderror");
            var funcpath = Xrm.Page.context.getClientUrl() + "/_grid/print/print_data.aspx";
            if (typeof ids !== "object") {
                var tids = ids;
                ids = new Array();
                ids.push(tids);
            }
            var wordTemplateId = wordtemplateid;//"f1f7b994-543b-e711-8106-c4346bac2908" test data;
            var currentEntityTypeCode = entitytypecodeint;//"10063" test data;
            var templateType = (templatetype || 9940); //9940 is global and 9941 is personal
            var fieldForFileName = (fieldforfilename || "");
            var formdata = "exportType=MergeWordTemplate&selectedRecords=" + encodeURIComponent(JSON.stringify(ids)) +
            "&associatedentitytypecode=" + currentEntityTypeCode + "&TemplateId=" + wordTemplateId + "&TemplateType=" + templateType;
            var req = new XMLHttpRequest();
            req.open("POST", funcpath, true);
            req.responseType = "arraybuffer";
            req.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            req.setRequestHeader("Accept-Language", "en-US,en;q=0.8");
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.onreadystatechange = function () {
                if (this.readyState == 4) {/* complete */
                    req.onreadystatechange = null;
                    if (this.status >= 200 && this.status <= 299) {//200 range okay
                        var mimetype = (2 === 2) ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        var blob = new Blob([req.response], { type: mimetype });
                        var fileNameTemplate = req.getResponseHeader('content-disposition').split('filename=')[1].replace(/'/g, "");
                        var dloadurl = URL.createObjectURL(blob);
                        var filename = (fieldForFileName !== "" && Xrm.Page.getAttribute(fieldForFileName) !== null && Xrm.Page.getAttribute(fieldForFileName).getValue() !== "") ?
                            Xrm.Page.getAttribute(fieldForFileName).getValue() : fileNameTemplate;
                        filename = filenameoverride || filename;
                        //new code, prevent IE errors
                        if (navigator.msSaveOrOpenBlob) {
                            navigator.msSaveOrOpenBlob(blob, filename);
                            return;
                        }
                        else if (window.navigator.msSaveBlob) { // for IE browser
                            window.navigator.msSaveBlob(blob, filename);
                            return;
                        }
                        var a = document.createElement("a");
                        document.body.appendChild(a);
                        a.style = "display: none";
                        a.href = dloadurl;
                        a.download = filename;
                        a.click();
                        URL.revokeObjectURL(dloadurl);
                        //window.location = dloadurl;//we can use just this instead of creating an anchor but we don't get to the name the file
                    }
                    else {
                        Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
                    }
                }
            };
            req.send(formdata);
        }
        catch (err) {
            Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists. " + err.message, "ERROR", "worderror");
        }

    }
like image 1
TeamEASI.com Avatar answered Oct 07 '22 05:10

TeamEASI.com


Just to simplify @TeamEASI.com answer a little here is what I did.

  1. Add a button to the ribbon using XRMToolBox Ribbon Workbench 2016. add button to ribbon
  2. Create a JS web resource like the one bellow.

/*
* Author:      Matthew Hunt
* File:        vsi_DownloadTemplate.js
* Date:        12/20/2017
* Project:     CRM USA
* Description: DownloadTemplate() allows the user to download a document template 
* via a button on the ribbon.
*
* @param entitytypecode: the type code of the entity. In the ribbon workbench set a
* CRM parameter with value PrimaryEntityTypeCode. ex: 1063
*
* @param  templateid: the id for the template you want to download. I had to go to 
* the database to find this and pass it as a string parameter in the ribbon workbench.
* For example: 
* SELECT DocumentTemplateId, Name FROM dbo.DocumentTemplateBase WHERE Name Like '%Quote%';
* returns something like 4AB391A4-D247-E711-80D3-005056914EA2
* Unforunatly, anytime the template is updated, you'll probably have to get the new id.
*
* @param templatetype: the code for the template type. Pass this value in the ribbon 
* workbench as a int param. ex: 9940 is a documenttemplate
* 
* @param filename: the resulting name of the file that will be downloaded to the users 
* computer. Pass this value in the ribbon workbench as a string param. ex: Quote.docx
*
*/
function DownloadTemplate(entitytypecode, templateid, templatetype, filename){
    
    // retrieve the entity id from the current page
    var entityid = new Array();
    entityid.push(Xrm.Page.data.entity.getId());
    
    // try and make a request for the document template
    try{
        
        // clear the page of any previous errors
        Xrm.Page.ui.clearFormNotification("docerror");
        
        // the path that will be used to retrieve the word template
        var funcpath = Xrm.Page.context.getClientUrl() + "/_grid/print/print_data.aspx";
        
        // open the request to create the template
        var req = new XMLHttpRequest();
        req.open("POST", funcpath, true);
        req.responseType = "arraybuffer";
        req.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        req.setRequestHeader("Accept-Language", "en-US,en;q=0.8");
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        
        // on completion, run the bellow function
        req.onreadystatechange = function () {
            // request complete
            if (this.readyState == 4) {
                req.onreadystatechange = null;
                 // check if we got back a 200 from the request
            if (this.status >= 200 && this.status <= 299) {
                
                // add the download url to an a tag and then click the a tag 
                // to download the document
                var mimetype = (2 === 2) ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                var blob = new Blob([req.response], { type: mimetype });
                var dloadurl = URL.createObjectURL(blob);
                var a = document.createElement("a");
                
                // if ie, because ie sucks
                if (navigator.msSaveOrOpenBlob) {
                    navigator.msSaveOrOpenBlob(blob, filename);
                    
                // else a browser that doesn't suck
                } else {
                    document.body.appendChild(a);
                    a.style = "display: none";
                    a.href = dloadurl;
                    a.download = filename;
                    a.click();
                    URL.revokeObjectURL(dloadurl);
                }
                
            }
        };
        
        // compile the data to send with the request
        var formdata = "exportType=MergeWordTemplate&selectedRecords=" + encodeURIComponent(JSON.stringify(entityid)) +
        "&associatedentitytypecode=" + entitytypecode + "&TemplateId=" + templateid + "&templatetype=" + templatetype;
        
        // make the request to create the template
        req.send(formdata);
        
    }catch (err) {
        PrintError(err.message);
    }
}

/*
* PrintError() is a helper method to display any errors to the user.
*/
function PrintError(msg){
    Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists. " + msg, "ERROR", "docerror");
}

IE fix: .click() giving access denied in IE11

  1. Create a command using XRMToolBox Ribbon Workbench 2016 with the following parameters to execute the JS when the button is clicked. Download command
like image 1
MasterProgrammer200 Avatar answered Oct 07 '22 05:10

MasterProgrammer200