Sunday, July 26, 2015

Call external web service from CRM 2011 using jquery

<!DOCTYPE html><html xmlns:s><head>
<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">  
    //Called this method on any button click  event for Testing
    $(document).ready(function(){
    $("button").click(function(){


 jQuery.support.cors = true;
 var soapData = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' + '<s:Body><GetData xmlns="http://tempuri.org/"><value>500</value></GetData></s:Body></s:Envelope>';

 $.ajax({
     type: "POST",
     contentType: "text/xml; charset=utf-8",
     dataType: "xml",
     url: "http://localhost:61373/Service1.svc",
     data: soapData,
     beforeSend: function (xhr) {
       xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IService1/GetData");
     },
     success: RetrieveCallbackSuccess,
     error: RetrieveCallbackError
 });
});

function RetrieveCallbackSuccess(response) {     alert("Yey! :) --> " + response.text); }
function RetrieveCallbackError(err) {     alert("Onoz! :( --> " + err.statusText); }

});
</script>

</head><body>

<button>tset</button>



</body></html>






WCF Service






Call external service from CRM webpage in CRM 2011

<!DOCTYPE html><html xmlns:soap><head>
<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
    //Called this method on any button click  event for Testing
    $(document).ready(function(){
    $("button").click(function(){


 var soapRequest="<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                 "<soap:Envelope xmlns:soap="+
                 "\"http://schemas.xmlsoap.org/soap/envelope/\" "+
                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+
                 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"<soap:Body>"+
"<GetData  xmlns=\'http://tempuri.org/\'>"+
"<value>500</value>"+
"</GetData>"+
" </soap:Body>"+
"</soap:Envelope>";

var xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

xmlHttp.open("post","http://localhost:61373/Service1.svc", false);

xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xmlHttp.setRequestHeader("Content-Length", soapRequest.length);
xmlHttp.setRequestHeader("SOAPAction","http://tempuri.org/IService1/GetData ");
xmlHttp.send(soapRequest);

var responseXML=xmlHttp.responseXML;
alert(responseXML.text);

});

});
</script>

</head><body>

<button>test</button>



</body></html>




WCF SERVICE





Display related data in CRM 2011


Retrieve Record Asynchronously using Soap in CRM 2011/2013

// <snippetSDK.RetrieveData.js>
if (typeof (SDK) == "undefined")
{
    SDK = { __namespace: true };
}
// Namespace container for functions in this library.
SDK.RetrieveData =
{
    _Context: function () {
        var errorMessage = "Context is not available.";
        if (typeof GetGlobalContext != "undefined") {
            return GetGlobalContext();
        }
        else {
            if (typeof Xrm != "undefined") {
                return Xrm.Page.context;
            }
            else {
                return new Error(errorMessage);
            }
        }
    },
    _ServerUrl: function () {///<summary>
        /// Private function used to establish the path to the SOAP endpoint based on context
        /// provided by the Xrm.Page object or the context object returned by the GlobalContext object.
        ///</summary>
        var ServerUrl = this._Context().getServerUrl();
        if (ServerUrl.match(/\/$/)) {
            ServerUrl = ServerUrl.substring(0, ServerUrl.length - 1);
        }
        return ServerUrl + "/XRMServices/2011/Organization.svc/web";
    },

    RetrieveRequestAsync: function (entityLogicalName, Id, columnSet, successCallBack, errorCallBack) {
        //        debugger;
        var request = "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        request += "<request i:type=\"a:RetrieveRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        request += "<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<b:key>Target</b:key>";
        request += "<b:value i:type=\"a:EntityReference\">";
        request += "<a:Id>" + Id + "</a:Id>";
        request += "<a:LogicalName>" + entityLogicalName + "</a:LogicalName>";
        request += "<a:Name i:nil=\"true\" />";
        request += "</b:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "<a:KeyValuePairOfstringanyType>";
        request += "<b:key>ColumnSet</b:key>";
        request += "<b:value i:type=\"a:ColumnSet\">";
        if (columnSet != null && columnSet.length > 0) {
            request += "<a:AllColumns>false</a:AllColumns>";
        }
        else {
            request += "<a:AllColumns>true</a:AllColumns>";
        }
        request += "<a:Columns xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
        if (columnSet != null && columnSet.length > 0) {
            for (var i = 0; i < columnSet.length; i++) {
                request += "<c:string>" + columnSet[i] + "</c:string>";
            }
        }
        request += "</a:Columns>";
        request += "</b:value>";
        request += "</a:KeyValuePairOfstringanyType>";
        request += "</a:Parameters>";
        request += "<a:RequestId i:nil=\"true\" />";
        request += "<a:RequestName>Retrieve</a:RequestName>";
        request += "</request>";
        request += "</Execute>";

        request = this._getSOAPWrapper(request);

        var req = new XMLHttpRequest();
        req.open("POST", this._ServerUrl(), true);
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", this._Action.Execute);
        req.onreadystatechange = function () { SDK.RetrieveData._returnRetrieveRequest(req, successCallBack, errorCallBack) };
        req.send(request);
    },


    _returnRetrieveRequest: function (resp, successCallBack, errorCallBack) {
        if (resp.readyState == 4 /* complete */) {
            if (resp.status == 200) {
                //Success
                //                var entityAttributesNodes = resp.responseXML.selectNodes("//a:Attributes/a:KeyValuePairOfstringanyType");

                //                var attributeCollection = new Array();
                //                for (var i = 0; i < entityAttributesNodes.length; i++) {
                //                    var attribute = this._getAttributeData(entityAttributesNodes[i]);
                //                    attributeCollection.push(attribute);
                //                }
                var attributeCollection = new Array();
                var xmlDoc = $.parseXML(resp.responseText);
                $xml = $(xmlDoc);
                //$xml.find("c\\:OptionMetadata, OptionMetadata")
                $xml.find('a\\:Attributes, Attributes').find('a\\:KeyValuePairOfstringanyType, KeyValuePairOfstringanyType').each(function () {
                    //attr
                    var attributeType = $(this).find("b\\:value, value")[0].attributes["i:type"].value; //KeyValuePairOfstringanyType.selectSingleNode("b:value").attributes.getNamedItem("i:type").text;
                    var attributeName = $(this).find("b\\:key, key").text(); //KeyValuePairOfstringanyType.selectSingleNode("b:key").text; //b:key

                    var attributeValue;
                    switch (attributeType) {
                        case "c:int":
                        case "c:string":
                        case "c:guid":
                            attributeValue = $(this).find("b\\:value, value").text(); //KeyValuePairOfstringanyType.selectSingleNode("b:value").text;
                            break;
                        case "a:Money":
                        case "a:OptionSetValue":
                        case "c:dateTime":
                        case "c:double":
                        case "c:decimal":
                        case "c:boolean":
                            //attributeValue = KeyValuePairOfstringanyType.parentNode.parentNode.selectNodes("a:FormattedValues/a:KeyValuePairOfstringstring[b:key='" + attributeName + "']/b:value")[0].text;
                            var temp = $($(this)[0].parentNode.parentNode).find("a\\:FormattedValues, FormattedValues").find("a\\:KeyValuePairOfstringstring, KeyValuePairOfstringstring").filter(function () {
                                return $(this).find("b\\:key, key").text() === attributeName;
                            });

                            attributeValue = $(temp).find("b\\:value, value").text();
                            //$($(temp)[0].parentNode).find("b\\:value").text();

                            break;
                        case "a:EntityReference":
                            //a:Name
                            attributeValue = $(this).find("b\\:value, value").find("a\\:Name, Name").text()//KeyValuePairOfstringanyType.selectSingleNode("b:value/a:Name").text;
                            break;
                        default:
                            attributeValue = $(this).find("b\\:value, value").text(); // KeyValuePairOfstringanyType.selectSingleNode("b:value").text;
                    }
                    var attribute = new Object();
                    attribute.attributeName = attributeName;
                    attribute.attributeValue = attributeValue;
                    attribute.displayName = "";
                    attributeCollection.push(attribute);
                });

                successCallBack(attributeCollection);
            }
            else {
                errorCallBack(this._getError(resp));
            }
        }
    },

    _getAttributeData: function (KeyValuePairOfstringanyType) {
        var attributeType = KeyValuePairOfstringanyType.selectSingleNode("b:value").attributes.getNamedItem("i:type").text;
        var attributeName = KeyValuePairOfstringanyType.selectSingleNode("b:key").text; //b:key
        var attributeValue;
        switch (attributeType) {
            case "c:int":
            case "c:string":
            case "c:guid":
                attributeValue = KeyValuePairOfstringanyType.selectSingleNode("b:value").text;
                break;
            case "a:Money":
            case "a:OptionSetValue":
            case "c:dateTime":
            case "c:double":
            case "c:decimal":
            case "c:boolean":
                attributeValue = KeyValuePairOfstringanyType.parentNode.parentNode.selectNodes("a:FormattedValues/a:KeyValuePairOfstringstring[b:key='" + attributeName + "']/b:value")[0].text
                break;
            case "a:EntityReference":
                //a:Name
                attributeValue = KeyValuePairOfstringanyType.selectSingleNode("b:value/a:Name").text;
                break;
            default:
                attributeValue = KeyValuePairOfstringanyType.selectSingleNode("b:value").text;
        }
        var attribute = new Object();
        attribute.attributeName = attributeName;
        attribute.attributeValue = attributeValue;
        attribute.displayName = "";
        return attribute;
    },

    _Action: {
        Execute: "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute"
    },
    _getSOAPWrapper: function (request) {
        ///<summary>
        /// Private function that wraps a soap envelope around a request.
        ///</summary>
        var SOAP = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body>";
        SOAP += request;
        SOAP += "</soapenv:Body></soapenv:Envelope>";
        return SOAP;
    },
    _getError: function (resp) {
        ///<summary>
        /// Private function that attempts to parse errors related to connectivity or WCF faults.
        ///</summary>
        ///<param name="resp" type="XMLHttpRequest">
        /// The XMLHttpRequest representing failed response.
        ///</param>

        //Error descriptions come from http://support.microsoft.com/kb/193625
        if (resp.status == 12029)
        { return new Error("The attempt to connect to the server failed."); }
        if (resp.status == 12007)
        { return new Error("The server name could not be resolved."); }
        var faultXml = resp.responseXML;
        var errorMessage = "Unknown (unable to parse the fault)";
        if (typeof faultXml == "object") {

            var bodyNode = faultXml.firstChild.firstChild;

            //Retrieve the fault node
            for (var i = 0; i < bodyNode.childNodes.length; i++) {
                var node = bodyNode.childNodes[i];

                //NOTE: This comparison does not handle the case where the XML namespace changes
                if ("s:Fault" == node.nodeName) {
                    for (var j = 0; j < node.childNodes.length; j++) {
                        var faultStringNode = node.childNodes[j];
                        if ("faultstring" == faultStringNode.nodeName) {
                            errorMessage = faultStringNode.text;
                            break;
                        }
                    }
                    break;
                }
            }
        }

        return new Error(errorMessage);

    },
    __namespace: true
};
// </snippetSDK.RetrieveData.js>

Create dynamic HTML and populate with CRM data




//LookupPreviewScript.js
///<reference path="XrmPage-vsdoc.js"/>
var XrmPage;
var popWindow;
var _VerticalLayout = "V";
var _HorizontalLayout = "H";
var _ZLayout = "Z";
var _NLayout = "N";

function LoadLookPreviewDelayed(webResourceId, lookupAttributeId, columnList, layout)
{
    myFn = function(){LoadLookPreview(webResourceId, lookupAttributeId, columnList, layout);}
    setTimeout(myFn, 2000);
    Xrm.Page.getAttribute(lookupAttributeId.toLowerCase()).addOnChange(myFn);
}

function FormHTMLTable(divTag, controlSet, layout)
{
    var str = "";
    if(layout == _VerticalLayout)
    {
        str = "<table><tbody>";
        for(var k=0; k < controlSet.length; k++){
        str += "<tr>";
        str += "<td class='headerCss'>" + controlSet[k].attributeDisplayName + "</td>";
        str += "<td>" + controlSet[k].attributeValue + "</td>";
        str += "</tr>";
        }
        str += "</tbody></table>";                            
    }
    else if(layout == _ZLayout)
    {
        str = '<table>';
        str += '<tbody>';
        for (var j = 0; j < controlSet.length; j++) {
            var index = j + 1;
            str += "<tr>"
            str += "<td class='headerCss'>" + controlSet[j].attributeDisplayName + "</td>";
            str += "<td>" + controlSet[j].attributeValue + "</td>";
            str += "<td class='seperator'></td>";
            if(index < controlSet.length)
            {
                str += "<td class='headerCss'>" + controlSet[index].attributeDisplayName + "</td>";
                str += "<td>" + controlSet[index].attributeValue + "</td>";
            }
            str += "</tr>"
            j++;
        }
        str += '</tbody>'
        str += '</table>';
    }
    else if(layout == _NLayout)
    {
        str = '<table>';
        str += '<tbody>';
        var half = Math.round(controlSet.length / 2)
        for (var j = 0; j < controlSet.length; j++) {
            var index = j + half;
            if (j < half) {
                str += "<tr>"
                str += "<td class='headerCss'>" + controlSet[j].attributeDisplayName + "</td>";
                str += "<td>" + controlSet[j].attributeValue + "</td>";
                str += "<td class='seperator'></td>";
                if (index < controlSet.length) {
                    str += "<td class='headerCss'>" + controlSet[index].attributeDisplayName + "</td>";
                    str += "<td>" + controlSet[index].attributeValue + "</td>";
                }
                str += "</tr>"
            }
        }
        str += '</tbody>'
        str += '</table>';
    }
    else
    {
        str = "<table><thead><tr>";
        for(var k=0; k < controlSet.length; k++){
        str += "<th>" + controlSet[k].attributeDisplayName + "</th>";
        }
        str += "</tr></thead><tbody><tr>";
        for (var i = 0; i < controlSet.length; i++){
        str += "<td>" + controlSet[i].attributeValue + "</td>";
        }
        str += "</tr></tbody></table>";
     }
     divTag.innerHTML = str;
     $(divTag).slideDown(1000);
}

function LoadLookPreview(webResourceId, lookupAttributeId, columnList, layout)
{
   var divTag;
   var webResource = Xrm.Page.getControl(webResourceId);
   var columnSet = new Array();
   var columnHeaders = new Array();
   var controlSet = new Array();

   successCallBack =  function (attributeCollection) {
                 for(var k=0; k < controlSet.length; k++)
                 {
                    for (var i = 0; i < attributeCollection.length; i++)
                    {
                        if(attributeCollection[i].attributeName == controlSet[k].attributeLogicalName)
                        {
                             controlSet[k].attributeValue = attributeCollection[i].attributeValue;
                             break;
                        }
                    }
                 }
               
                 FormHTMLTable(divTag, controlSet, layout);
          };

        errorCallBack =  function (error) {
             divTag.innerHTML = "<span class='error'>" + error.message + "</span>";
             $(divTag).slideDown(500);
         };

   if(webResource == null)
   {
       alert('No WebResource found with Id = ' + webResourceId);
   }
   else
   {
        divTag = document.getElementById(webResourceId).contentWindow.document.getElementById('lookupDIV');
        if(divTag != null)
        {
            divTag.display = "none";
            var columns = columnList.split('|');
            for(var i = 0 ; i < columns.length; i++)
            {
                var s = columns[i].split('#');
                var _control = new Object();
                _control.attributeDisplayName = s[0];
                _control.attributeLogicalName = s[1].toLowerCase().replace(/^\s+|\s+$/g, '')
                _control.attributeValue = '';
                controlSet.push(_control);

                columnSet.push(s[1].toLowerCase().replace(/^\s+|\s+$/g, ''));
            }

            if(Xrm.Page.getAttribute(lookupAttributeId.toLowerCase()) != null && Xrm.Page.getAttribute(lookupAttributeId.toLowerCase()).getValue() != null)
            {
                var id = Xrm.Page.getAttribute(lookupAttributeId).getValue()[0].id;
                var entityName = Xrm.Page.getAttribute(lookupAttributeId).getValue()[0].entityType;
                SDK.RetrieveData.RetrieveRequestAsync(entityName, id, columnSet, successCallBack, errorCallBack);
            }
            else
            {
                FormHTMLTable(divTag, controlSet, layout);
            }
        }
        else
        {
            alert("Error in Loading Web Resource (DIV is NULL)")
        }
   }
}