Dynamics CRM 2011 · Javascript

CRM 2011: Retrieve Entity Metadata using Javascript

It is rare, but I had encountered the need to retrieve entity metadata using Javascript. Here’s how.

This example function retrieves entity metadata to get object type code given entity logical name.

function getEntityTypeCodeFromLogicalName(entityName) {
    /*Generate Soap Body.*/
    var soapBody = "<soap:Body>" +
                    "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                        "<request i:type=\"a:RetrieveEntityRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">" +
                        "<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
                        "<a:KeyValuePairOfstringanyType>" +
                          "<b:key>EntityFilters</b:key>" +
                          "<b:value i:type=\"c:EntityFilters\" xmlns:c=\"http://schemas.microsoft.com/xrm/2011/Metadata\">" + "Entity" + "</b:value>" +
                        "</a:KeyValuePairOfstringanyType>" +

                        "<a:KeyValuePairOfstringanyType>" +
                          "<b:key>MetadataId</b:key>" +
                          "<b:value i:type=\"ser:guid\"  xmlns:ser=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + "00000000-0000-0000-0000-000000000000" + "</b:value>" +
                        "</a:KeyValuePairOfstringanyType>" +

                        "<a:KeyValuePairOfstringanyType>" +
                          "<b:key>RetrieveAsIfPublished</b:key>" +
                          "<b:value i:type=\"c:boolean\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + "false" + "</b:value>" +
                        "</a:KeyValuePairOfstringanyType>" +

                        "<a:KeyValuePairOfstringanyType>" +
                          "<b:key>LogicalName</b:key>" +
                          "<b:value i:type=\"c:string\"   xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + entityName + "</b:value>" +
                        "</a:KeyValuePairOfstringanyType>" +
                        "</a:Parameters>" +
                        "<a:RequestId i:nil=\"true\" /><a:RequestName>RetrieveEntity</a:RequestName></request>" +
                    "</Execute>" +
                    "</soap:Body>";

    /*Wrap the Soap Body in a soap:Envelope.*/
    var soapXml = "<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'>" +
                soapBody +
                "</soap:Envelope>";

    /* Create the XMLHTTP object for the execute method.*/
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", Xrm.Page.context.getServerUrl() + '/XRMServices/2011/Organization.svc/web', false);
    xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*");
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    xmlhttp.setRequestHeader("Content-Length", soapXml.length);

    /* Send the XMLHTTP object. */
    xmlhttp.send(soapXml);
    var response = xmlhttp.status;
    if (response == "200") {
        var result = xmlhttp.responseText;

        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(result);

        return xmlDoc.getElementsByTagName("c:ObjectTypeCode")[0].childNodes[0].nodeValue;
    }
    
    return null;
}

Notice we are connecting to '/XRMServices/2011/Organization.svc/web' instead of '/MSCRMservices/2007/CrmService.asmx', hence we no longer need to use Xrm.Page.context.getAuthenticationHeader().

Cheers – Sy

Advertisements

One thought on “CRM 2011: Retrieve Entity Metadata using Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s