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

时间:2015-07-20 19:00:56

标签: javascript c# soap crm dynamics-crm-2015

我有一个场景,我需要在功能区按钮上单击触发插件,我已经设置了命令和按钮,我已经创建了一个自定义动作,将在插件注册工具中注册以连接事件。我测试了一个简单的hello世界的功能区按钮,所以我确信基本功能是用餐和设置。我的问题在于我尝试调用执行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, "");

1 个答案:

答案 0 :(得分:0)

我没看到定义函数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,操作注册的实体的名称以及Action的名称(在这种情况下是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