[英]CRM Dynamics 2015 Ribbon Button Trigger plugin using JavaScript
I have a scenario where I need to trigger a plugin on a ribbon button click, I have setup the commands and buttons, I have created a custom action,that will be registered in the plugin reg tool to wire up the event. 我有一个场景,我需要在功能区按钮单击时触发插件,我已经设置了命令和按钮,我创建了一个自定义动作,该动作将在插件reg工具中注册以连接事件。 I have tested the ribbon button that just does a simple hello world, so I am sure that the basics are dine and set.
我已经测试了功能区按钮,它仅是一个简单的hello world,因此我确信基本操作和设置都正确。 My issue lies when I try to call the function that does a SOAP invocation.
我的问题出在我尝试调用执行SOAP调用的函数时。 Here is my code below.
这是下面的代码。
JavaScript JavaScript的
function 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, "");
I dont see defined the function GetServiceUrl() 我看不到定义函数GetServiceUrl()
req.open("POST", GetServiceUrl(), false)
Here is an example how to do it ;) 这是一个如何做的例子;)
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;
}
Next, we need to create a function that will execute our action and pass the record's GUID, the name of the entity the action is registered against, and the Action's name (requestName, in this case). 接下来,我们需要创建一个函数,该函数将执行操作并传递记录的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 http://www.plusconsulting.com/Blogs/Lists/Posts/Post.aspx?ID=56
or 要么
CRM 2013: Calling actions from javascript CRM 2013:通过JavaScript调用操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.