Daren's profileDaren TurnerBlogLists Tools Help

Blog


    15 December

    How to pull entity information from the Microsoft CRM MetaData Service using javascript.

     

    The following code will pull entity information, using the Metadata service, using javascript.  This code is useful for pulling the objecttypecode dynamically when they may differ between environments.

    var _sWebServicesNamespace = "http://schemas.microsoft.com/crm/2006/WebServices";

    function _RemoteMetaCommand(sCommand, sAction)
    {
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    xmlhttp.open("POST", "/mscrmservices/2006/metadataservice.asmx" , false);
    xmlhttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", _sWebServicesNamespace + "/" + sAction);

    var soapmessage = "<?xml version='1.0' encoding='utf-8'?>"
    soapmessage += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
    soapmessage += "<soap:Body>" + sCommand + "</soap:Body></soap:Envelope>"
    xmlhttp.send(soapmessage);

    return xmlhttp.responseXML;
    }

    var entity = "new_country"
    var query = "<RetrieveEntityMetadata xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>"
    query += "<entityName>" + entity + "</entityName><flags>EntityOnly</flags></RetrieveEntityMetadata>"

    //Get ObjectTypeCode

    var result = _RemoteMetaCommand(query, "RetrieveEntityMetadata").selectSingleNode("//ObjectTypeCode");

    var countryentitytypecode = result.text;

     

    Here is the response.