簡體   English   中英

Dynamics CRM 365:通過功能區上的按鈕下載 Word 文檔模板

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

目前用戶必須點擊省略號、單詞模板、最后引用才能下載單詞模板。

省略號,單詞模板,最后引用

為了方便我們的用戶,我們希望在按下功能區上的“打印報價”按鈕時下載文檔。

打印報價

這可能嗎? 如果是這樣,我將如何去做? 我了解如何使用功能區工作台編輯功能區。 我需要知道如何使用功能區下載單詞模板。 絲帶工作台

如果解決方案是使用ribbon workbench,我可以輸入什么命令來下載word模板?

當您單擊模板彈出按鈕時,它會通過調用/AppWebServices/DocumentTemplate.asmx動態填充,該調用返回菜單的 XML。

事件主頁網格中 Word 模板的彈出窗口如下所示:

<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>

我目前沒有辦法嘗試它,但我會嘗試“復制”最后一個<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" />

可以僅使用受支持的 CRM 功能來執行此操作(當然,我確信也可以使用不受支持的 javascript,但我目前沒有時間對此進行調查)。 您應該采取的步驟來實現您想要的功能:

  1. 創建類型為 Action 的新流程,綁定到您要為其創建模板的實體(我在這里建議 Action 的原因是它可以使用 JavaScript 和 CRM WebAPI 輕松調用)
  2. 在這個動作中添加一個步驟 - 調用一個動作並選擇內置動作“SetWordTemplate”
  3. 設置此操作的屬性 - 選擇您需要的模板並將目標動態設置為當前實體(使用動態值助手)如果您從未使用過此操作 - 它只是創建一個給定的單詞模板並將其作為注釋添加到您的實體
  4. 現在您需要在按鈕內編寫邏輯(我假設您知道如何使用 Ribbon Workbench 或其他工具添加按鈕)
  5. 使用 WebAPI 調用您的操作
  6. 使用附加文檔查找剛剛為您的實體創建的注釋
  7. 下載附件(您可以向用戶顯示一些提示或只是強制下載文件,用戶必須保存它)
  8. 刪除注釋

也許不是單線,但讓你在受支持的區域......

 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"); } }

只是為了簡化@TeamEASI.com 的回答,這就是我所做的。

  1. 使用 XRMToolBox Ribbon Workbench 2016 向功能區添加按鈕。 將按鈕添加到功能區
  2. 創建一個像下面這樣的 JS web 資源。

 /* * 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 修復: .click() 在 IE11 中拒絕訪問

  1. 使用 XRMToolBox Ribbon Workbench 2016 創建一個帶有以下參數的命令,以便在單擊按鈕時執行 JS。 下載命令

要獲取實體代碼,請運行以下查詢:

SELECT coalesce(OriginalLocalizedName,name) AS DisplayName, Name AS SchemaName, ObjectTypeCode
FROM EntityLogicalView
ORDER BY ObjectTypeCode

Template ID

SELECT DocumentTemplateId, Name FROM dbo.DocumentTemplateBase

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM