使用 JavaScript 的 CRM Dynamics 2015 功能区按钮触发器插件

CRM Dynamics 2015 Ribbon Button Trigger plugin using JavaScript

我有一个场景,我需要在点击功能区按钮时触发一个插件,我已经设置了命令和按钮,我已经创建了一个自定义操作,它将在插件注册工具中注册以连接事件。我已经测试了功能区按钮,它只是一个简单的 hello world,所以我确信基础知识已经准备就绪。我的问题出在我尝试调用执行 SOAP 调用的函数时。下面是我的代码。

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

我没有看到定义函数 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: Calling actions from javascript