Wednesday, May 20, 2015

Full example - get Contact Details base Fetch\Soap – Client side

function FormOnLoad() {
    try {
        if (Xrm.Page.data.entity.getId() == null) { return true; }
        var contactId = Xrm.Page.data.entity.getId();
        var fetch = BuilFetchForContact(contactId);
        var result = ExecuteFetchRequest(fetch);
        alert(result[0].fullname.Value);
    }
    catch (ex) {
        alert(ex.message);
    }
}
function BuilFetchForContact(contactId) {
    var fetch = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">';
    fetch +=        '<entity name="contact">';
    fetch +=            '<attribute name="fullname" />';
    fetch +=            '<attribute name="telephone1" />';
    fetch +=            '<attribute name="contactid" />';
    fetch +=            '<filter type="and">';
    fetch +=                '<condition attribute="contactid" operator="eq"  value="' + contactId + '" />';
    fetch +=            '</filter>';
    fetch +=        '</entity>';
    fetch +=    '</fetch>';
    return fetch;
}

function ExecuteFetchRequest (fetch) {
    var fetchFormatedString = CrmEncodeDecode.CrmXmlEncode(fetch);
    var requestMain = "";
    requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestMain += "  <s:Body>";
    requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += "      <request i:type=\"b:ExecuteFetchRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>FetchXml</c:key>";
    requestMain += "            <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + fetchFormatedString + "</c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "        </a:Parameters>";
    requestMain += "        <a:RequestId i:nil=\"true\" />";
    requestMain += "        <a:RequestName>ExecuteFetch</a:RequestName>";
    requestMain += "      </request>";
    requestMain += "    </Execute>";
    requestMain += "  </s:Body>";
    requestMain += "</s:Envelope>";

    try {
        var req = new XMLHttpRequest();
    }
    catch (e) {
        var req = new ActiveXObject("Msxml2.XMLHTTP");
    }

    var isAsync = false;

    req.open("POST", getServerUrlWithOrgServicePath(), isAsync)
    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");
    try { req.responseType = 'msxml-document'; } catch (e) { }
    req.send(requestMain);
    return CreateResultsArray(req);
}

function getServerUrlWithOrgServicePath () {
    var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
    var serverUrl = Xrm.Page.context.getClientUrl();
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);

    }
    return serverUrl + OrgServicePath;
}

function CreateResultsArray (request) {
    var createResultsArrayNSResolver = { a: "http://schemas.microsoft.com/xrm/2011/Contracts", c: "http://schemas.datacontract.org/2004/07/System.Collections.Generic" };

    var xmlDocument = XUI.Xml.LoadXml(request.responseText.replace('xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services"', ''));

    var fetchResultsText = XUI.Xml.GetText(XUI.Xml.SelectSingleNode(xmlDocument, "//a:Results/a:KeyValuePairOfstringanyType/c:value", createResultsArrayNSResolver));
    var fetchResultsNode = XUI.Xml.LoadXml(fetchResultsText);

    var arr = XUI.Xml.SelectNodes(fetchResultsNode, "//result", null);
    var retArr = new Array();
    for (var i = 0; i < arr.length; i++) {
        var resultObj = {};
        for (var j = 0; j < arr[i].childNodes.length; j++) {
            if (arr[i].childNodes[j].nodeType != 1)
                continue;
            var fixedNodeName = arr[i].childNodes[j].tagName.replace(".", "_");
            resultObj[fixedNodeName] = {};
            resultObj[fixedNodeName].Value = XUI.Xml.GetText(arr[i].childNodes[j]);
            resultObj[fixedNodeName].Attributes = {};
            for (var k = 0; k < arr[i].childNodes[j].attributes.length; k++) {
                resultObj[fixedNodeName].Attributes[arr[i].childNodes[j].attributes[k].name] = XUI.Xml.GetText(arr[i].childNodes[j].attributes[k]);
            }
        }
        retArr.push(resultObj);
    }
    return retArr;
}

No comments:

Post a Comment