當前用戶必須單擊省略號,單詞模板並最後引用才能下載單詞模板。Dynamics CRM 365:通過功能區上的按鈕下載Word文檔模板
爲了使其更容易爲我們的用戶,我們希望按「打印報價」按鈕,在功能區的時候有文件的下載。
這可能嗎?如果是的話,我會怎麼做呢?我瞭解如何使用功能區工作臺編輯功能區。我需要知道如何使用功能區下載單詞模板。
如果解決方案使用功能區工作臺,我可以輸入什麼命令來獲取要下載的單詞模板?
當前用戶必須單擊省略號,單詞模板並最後引用才能下載單詞模板。Dynamics CRM 365:通過功能區上的按鈕下載Word文檔模板
爲了使其更容易爲我們的用戶,我們希望按「打印報價」按鈕,在功能區的時候有文件的下載。
這可能嗎?如果是的話,我會怎麼做呢?我瞭解如何使用功能區工作臺編輯功能區。我需要知道如何使用功能區下載單詞模板。
如果解決方案使用功能區工作臺,我可以輸入什麼命令來獲取要下載的單詞模板?
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");
}
}
a.click();在Internet Explorer上拋出拒絕訪問錯誤,但從Chrome運行時,Filed正在下載,但它顯示「文件已損壞,無法打開」。任何想法如何處理這兩個問題。 – AnkUser
此外,如果它幫助那些正在使用CRM模板的人,wordtemplateid會隨時更改您必須對模板進行編輯,這是不好的。所以,我已經爲您製作了一個CRM解決方案,您可以使用下面的鏈接下載該解決方案。該解決方案非常簡單,您可以下載和更新現有模板,因此無需更改ID。 http://teameasi.com/blog/365-document-template-downloader –
AnkUser - 我修復了IE錯誤,但在Chrome問題上我無法重現。你確定你的單詞模板沒有損壞嗎?你可以在沒有問題的情況下使用本地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" />
謝謝你的回答。我遇到了這樣的問題,即當按鈕被添加到帶有與下拉列表中的Quote項目相同的ID的功能區時,它們都不起作用,因爲它們中只有一個可以一次具有相同的ID。 – MasterProgrammer200
身份證必須始終是唯一的,我只是做一些事情,即'Id =」vsi.quote.Download.Button「' – Alex
不幸的是,身份證似乎是導致下載的原因,因爲它具有文檔模板的標識。刪除該ID並且不進行下載。 – MasterProgrammer200
這是可能做到這一點使用CRM(當然我敢肯定,這也有可能使用不支持JavaScript的做的,但我沒有時間目前對此展開調查)的唯一支持的功能。你應該實現的功能的步驟,你想:
也許不是一行代碼,而是讓您在支持的區域...
謝謝你的回答。對不起,我遲到了,我有另一個項目,我不得不在上週工作。我正在嘗試您的解決方案,到目前爲止,我已將其轉到步驟6.功能區按鈕將報價另存爲備註。但是,api返回204 No Content,所以我沒有得到新註釋的id。你會如何建議我得到新創建的筆記的ID,以便我可以調用Web API來下載單詞doc然後刪除? – MasterProgrammer200
只是爲了簡化@ TeamEASI.com回答這裏一點是我做的。
/*
* 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修復:
a.click(); 在Internet Explorer上拋出拒絕訪問錯誤,但從Chrome運行時,Filed正在下載,但它顯示「文件已損壞,無法打開」。 任何想法如何處理這兩個問題。 – AnkUser
@AnkUser我現在也遇到了這兩個問題。我將調查並希望在下週內解決新問題。 – MasterProgrammer200
@AnkUser對於第一個問題,請檢查以確保您的數據庫中的報價ID是正確的。如果它說文件已損壞,並且您從文本編輯器中打開.doc文件,您會注意到它是一個html文件。對於IE問題,請查看我的更新代碼。我添加了一個if語句來爲IE運行一些特殊的代碼。 – MasterProgrammer200
的可能的複製[如何自定義在Microsoft Dynamics CRM菜單選項卡?](https://stackoverflow.com/questions/44515209/how-to-customize-menu-tab-in-microsoft-dynamics -rr) –
不,它不是完全重複的。相同的功能區/命令欄定製,但這是更真實的.. –
@JacobH這不是重複。我知道如何編輯功能區。我需要知道如何使用功能區上的按鈕下載單詞模板。我更新了我的問題。見上面^。 – MasterProgrammer200