代码之家  ›  专栏  ›  技术社区  ›  Sergii Gorkun

我如何重新制作FetchUtil.js for CRM 2011 UR 12

  •  2
  • Sergii Gorkun  · 技术社区  · 12 年前

    我必须重新制作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);
    

    由于.selectSingleNode(“//a:Entities”).xml,它现在甚至在IE中也不起作用 我是这样做的,但没有xml字段。

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

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

    3 回复  |  直到 12 年前
        1
  •  2
  •   Peter    12 年前

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

    (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…我大部分的fetch调用都是在网络资源的html页面中进行的,所以这不是问题)这在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和KeyValuePair OfstringanyType的属性,但您可以使用正确的选择器组合解析出任何属性

    检索到的每个项目

        2
  •  2
  •   Krutika Suchak    10 年前

    我也遇到了类似的问题,我使用下面的解决方法解决了这个问题。

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

    当做 克鲁蒂卡·苏恰克

        3
  •  0
  •   Daryl    12 年前

    如果您正在寻找一个不需要JQuery的版本,以及一个解析结果的版本, check this out 。它不仅封装了FetchXML,而且还将响应XML解析为JavaScript对象,以便于检索。