CRM Dynamics 2015功能区按钮触发插件使用JavaScript
本文关键字:插件 JavaScript Dynamics 2015 功能区 按钮 CRM | 更新日期: 2023-09-27 18:03:33
我有一个场景,我需要触发一个插件上的ribbon按钮单击,我已经设置了命令和按钮,我已经创建了一个自定义动作,这将在插件reg工具中注册,以连接事件。我已经测试了ribbon按钮,它只做了一个简单的hello世界,所以我确信基本的已经设置好了。我的问题在于当我尝试调用执行SOAP调用的函数时。下面是我的代码。
JavaScriptfunction ExecutePhoneCall(requestName, refreshPage, stringParameter) {
// Creating the request XML for calling the Action
var requestXML = ""
if (stringParameter == null) {
requestXML += "<s:Envelope xmlns:s='"http://schemas.xmlsoap.org/soap/envelope/'">";
requestXML += "<s:Body>";
requestXML += "<Execute xmlns='"http://schemas.microsoft.com/xrm/2011/Contracts/Services'"xmlns:i='"http://www.w3.org/2001/XMLSchema-instance'">";
requestXML += "<request xmlns:a='"http://schemas.microsoft.com/xrm/2011/Contracts'">";
requestXML += "<a:Parameters xmlns:b='"http://schemas.datacontract.org/2004/07/System.Collections.Generic'">";
requestXML += " <a:KeyValuePairOfstringanyType>";
requestXML += " <b:key>Target</b:key>";
requestXML += " <b:value i:type='"a:EntityReference'">";
requestXML += " <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";
requestXML += " <a:LogicalName>" + Xrm.Page.data.entity.getEntityName() + "</a:LogicalName>";
requestXML += " <a:Name i:nil='"true'" />";
requestXML += " </b:value>";
requestXML += " </a:KeyValuePairOfstringanyType>";
requestXML += " </a:Parameters>";
requestXML += " <a:RequestId i:nil='"true'" />";
requestXML += " <a:RequestName>" + requestName + "</a:RequestName>";
requestXML += " </request>";
requestXML += " </Execute>";
requestXML += " </s:Body>";
requestXML += "</s:Envelope>";
}
else {
requestXML += "<s:Envelope xmlns:s='"http://schemas.xmlsoap.org/soap/envelope/'">";
requestXML += " <s:Body>";
requestXML += " <Execute xmlns='"http://schemas.microsoft.com/xrm/2011/Contracts/Services'"xmlns:i='"http://www.w3.org/2001/XMLSchema-instance'">";
requestXML += " <request xmlns:a='"http://schemas.microsoft.com/xrm/2011/Contracts'">";
requestXML += " <a:Parameters xmlns:b='"http://schemas.datacontract.org/2004/07/System.Collections.Generic'">";
requestXML += " <a:KeyValuePairOfstringanyType>";
requestXML += " <b:key>Target</b:key>";
requestXML += " <b:value i:type='"a:EntityReference'">";
requestXML += " <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";
requestXML += " <a:LogicalName>" + Xrm.Page.data.entity.getEntityName() + "</a:LogicalName>";
requestXML += " <a:Name i:nil='"true'" />";
requestXML += " </b:value>";
requestXML += " </a:KeyValuePairOfstringanyType>";
requestXML += " <a:KeyValuePairOfstringanyType>";
requestXML += " <b:key>Data</b:key>";
requestXML += " <b:value i:type='"c:string'" xmlns:c='"http://www.w3.org/2001/XMLSchema'">" + stringParameter + "</b:value>";
requestXML += " </a:KeyValuePairOfstringanyType>";
requestXML += " </a:Parameters>";
requestXML += " <a:RequestId i:nil='"true'" />";
requestXML += " <a:RequestName>" + requestName + "</a:RequestName>";
requestXML += " </request>";
requestXML += " </Execute>";
requestXML += " </s:Body>";
requestXML += "</s:Envelope>";
}
var req = new XMLHttpRequest();
req.open("POST", GetServiceUrl(), false)
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(requestXML);
//refresh the page if the request was successful.
if (req.status == 200) {
if (refreshPage) {
RefreshForm()
}
}
else {
Xrm.Utility.alertDialog(req.statusText + "'n" + req.responseXML.getElementsByTagName("faultstring")[0].textContent);
}
}
function RefreshForm() {
Xrm.Utility.openEntityForm(Xrm.Page.data.entity.getEntityName(), Xrm.Page.data.entity.getId());
}
ExecutePhoneCall("new_ActivatePhonecall", true, "");
我没有看到定义函数GetServiceUrl()
req.open("POST", GetServiceUrl(), false)
这是一个如何做的例子;)
function ExecuteAction(entityId, entityName, requestName) {
// Creating the request XML for calling the Action
var requestXML = ""
requestXML += "<s:envelope xmlns:s="'"http://schemas.xmlsoap.org/soap/envelope/'"">";
requestXML += " <s:body>";
requestXML += " <execute xmlns="'"http://schemas.microsoft.com/xrm/2011/Contracts/Services'"" xmlns:i="'"http://www.w3.org/2001/XMLSchema-instance'"">";
requestXML += " <request xmlns:a="'"http://schemas.microsoft.com/xrm/2011/Contracts'"">";
requestXML += " <a:parameters xmlns:b="'"http://schemas.datacontract.org/2004/07/System.Collections.Generic'"">";
requestXML += " <a:keyvaluepairofstringanytype>";
requestXML += " <b:key>Target</b:key>";
requestXML += " <b:value i:type="'"a:EntityReference'"">";
requestXML += " <a:id>" + entityId + "</a:id>";
requestXML += " <a:logicalname>" + entityName + "</a:logicalname>";
requestXML += " <a:name i:nil="'"true'"">";
requestXML += " </a:name></b:value>";
requestXML += " </a:keyvaluepairofstringanytype>";
requestXML += " </a:parameters>";
requestXML += " <a:requestid i:nil="'"true'"">";
requestXML += " <a:requestname>" + requestName + "</a:requestname>";
requestXML += " </a:requestid></request>";
requestXML += " </execute>";
requestXML += " </s:body>";
requestXML += "</s:envelope>";
var req = new XMLHttpRequest();
req.open("POST", GetClientUrl(), false)
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(requestXML);
//Get the Response from the CRM Execute method
//var response = req.responseXML.xml;
}
function GetClientUrl() {
if (typeof Xrm.Page.context == "object") {
clientUrl = Xrm.Page.context.getClientUrl();
}
var ServicePath = "/XRMServices/2011/Organization.svc/web";
return clientUrl + ServicePath;
}
接下来,我们需要创建一个函数,该函数将执行我们的操作并传递记录的GUID、注册操作的实体的名称和操作的名称(在本例中为requestName)。
function openPhoneCall() {
var entityId = Xrm.Page.data.entity.getId();
var entityName = "phonecall";
var requestName = "new_ReactivatePhoneCall";
ExecuteAction(entityId, entityName, requestName);
window.location.reload(true);
}
http://www.plusconsulting.com/Blogs/Lists/Posts/Post.aspx?ID=56 或
CRM 2013:从javascript调用操作