Archive for December, 2008

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

December 15, 2008 6 comments


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 = "";

function _RemoteMetaCommand(sCommand, sAction)
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");"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=’’ xmlns:xsd=’’ xmlns:soap=’’>"
soapmessage += "<soap:Body>" + sCommand + "</soap:Body></soap:Envelope>"

return xmlhttp.responseXML;

var entity = "new_country"
var query = "<RetrieveEntityMetadata xmlns=’’>"
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.

Categories: Uncategorized