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

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.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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 )

Connecting to %s

%d bloggers like this: