2

我必须重新制作 FetchUtil.js 才能在 CRM 2011 UR 12 中使用它。我的 JavaScript 不是很好,所以我需要一些帮助。

这是本机代码

 var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities").xml;
 var resultDoc = new ActiveXObject("Microsoft.XMLDOM");
 resultDoc.async = false;
 resultDoc.loadXML(sFetchResult);

它现在即使在 IE 中也不起作用,因为 .selectSingleNode("//a:Entities").xml 我这样做了,但是那里没有 xml 字段。

sFetchResult = xmlhttp.responseXML.getElementsByTagName('a:Entities')[0].xml;
    var resultDoc = new ActiveXObject("Microsoft.XMLDOM");
    resultDoc.async = false;
    resultDoc.loadXML(sFetchResult);

帮我为 IE 和 Chrome 重新制作这个。非常感谢!

4

3 回答 3

2

这是我的调用模块(包括为网络资源)

(function (module, undefined) {

    module.buildFetchRequest = function (fetch) {
        /// <summary>
        /// builds a properly formatted FetchXML request
        /// based on Paul Way's blog post "Execute Fetch from JavaScript in CRM 2011"
        /// http://blog.customereffective.com/blog/2011/05/execute-fetch-from-javascript-in-crm-2011.html
        /// </summary>
        var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        request += "<s:Body>";

        request += '<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">' +
            '<request i:type="b:RetrieveMultipleRequest" ' +
            ' xmlns:b="http://schemas.microsoft.com/xrm/2011/Contracts" ' +
            ' xmlns:i="http://www.w3.org/2001/XMLSchema-instance">' +
            '<b:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">' +
            '<b:KeyValuePairOfstringanyType>' +
            '<c:key>Query</c:key>' +
            '<c:value i:type="b:FetchExpression">' +
            '<b:Query>';

        request += CrmEncodeDecode.CrmXmlEncode(fetch);

        request += '</b:Query>' +
            '</c:value>' +
            '</b:KeyValuePairOfstringanyType>' +
            '</b:Parameters>' +
            '<b:RequestId i:nil="true"/>' +
            '<b:RequestName>RetrieveMultiple</b:RequestName>' +
            '</request>' +
            '</Execute>';

        request += '</s:Body></s:Envelope>';
        return request;
    };

    module.sendFetchQuery = function (fetchRequest, doneCallback, failCallback) {
        //path to CRM root
        var server = window.location.protocol + "//" + window.location.host;

        //full path to CRM organization service - you may need to modify this depending on your particular situation
        var path = server + "/XRMServices/2011/Organization.svc/web";

        $.ajax({
            type: "POST",
            dataType: 'xml',
            async: false,
            contentType: "text/xml; charset=utf-8",
            processData: false,
            url: path,
            data: fetchRequest,
            beforeSend: function (xhr) {
                xhr.setRequestHeader(
                    "SOAPAction",
                    "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute"
                ); //without the SOAPAction header, CRM will return a 500 error
            }
        }).done(doneCallback)
          .fail(failCallback);

    };

}(window.xFetch = window.xFetch || {}));

用法(解析器需要 jQuery ...我在 web 资源 html 页面中进行大部分 fetch 调用,所以这不是问题)这在 IE 中有效,Chrome 没有检查 firefox,但我不明白为什么它会不行。

   var fetchXml =
                xFetch.buildFetchRequest("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                    "   <entity name='ENTITYNAME'>" +
                    "    <attribute name='ATTRIBUTE' />" +
                    "  </entity>" +
                    "</fetch>");

            var entityList = new Array();

            xFetch.sendFetchQuery(fetchXml,
                function (fetchResponse) {

                    // chrome doesn't like the namespaces because of 
                    // selectSingleNode implementations (which make sense btw)
                    // I'll never understand why Microsoft have to pepper their xml 
                    // with namespace dross
                    $(fetchResponse).find("a\\:Entity, Entity").each(function () {

                        var entityData = {};

                        $(this).find("a\\:KeyValuePairOfstringanyType, KeyValuePairOfstringanyType").each(function () {
                            var xmlElement = $(this);
                            var key = xmlElement.find("b\\:key, key").text();
                            var value = xmlElement.find("b\\:value, value").text();
                            entityData[key] = value;
                        });

                        //inner loop
                        $(this).find("a\\:KeyValuePairOfstringstring, KeyValuePairOfstringstring").each(function () {
                            var xmlElement = $(this);
                            var key = xmlElement.find("b\\:key, key").text();
                            var value = xmlElement.find("b\\:value, value").text();
                            entityData[key] = value;
                        });

                        entityList.push(entityData);
                    });

                }, function (jqXhr, textStatus, errorThrown) {
                    // if unsuccessful, generate an error alert message
                });


            for (var i = 0; i < entityList.length; i++) {

                if (entityList[i].ATTRIBUTE === "Yes" ){ 
                   // DO WHATEVER
                }
            }

我只需要带有 KeyValuePairOfstringstring 和 KeyValuePairOfstringanyType 的属性,但是您可以使用正确的选择器组合解析出任何属性

检索到的每个项目

于 2013-04-10T22:44:44.483 回答
2

我遇到了类似的问题,我通过使用以下解决方法解决了它。

var sFetchResult = xmlhttp.response;
var tempresultDoc = new ActiveXObject("Microsoft.XMLDOM");
tempresultDoc.async = false;
tempresultDoc.loadXML(sFetchResult);

// 现在,我们将拥有 XML 文件。使用以下代码从 XML 中获取 singleNode。

var resultDoc = new ActiveXObject("Microsoft.XMLDOM");
resultDoc.async = false;
resultDoc.loadXML(tempresultDoc.childNodes[0].selectSingleNode("//a:Entities").xml);

问候,克鲁蒂卡·苏查克

于 2015-02-26T10:21:00.593 回答
0

如果您正在寻找不需要 JQuery 的版本以及解析结果的版本,请查看。它不仅包装了 FetchXML,还将响应 XML 解析为 JavaScript 对象以便于检索。

于 2013-07-03T12:48:34.277 回答