How to query the Metadata Service via JavaScript

A very useful post

Example (Jscript) –

/* ********************************************************
gQueryMetadataService() function
Description: Generic function to call the metadata webservice
Calls by : gGetEntityProperty
Returns: XML response array
parameters for examole:
– MetadataItems – The items you wish to retrieve e.g. EntitiesOnly, IncludeAttributes, IncludePrivileges, IncludeRelationships and All.
– RetrieveAsIfPublished – specifies whether you want to get a list of just the published entity metadata or all of the entity metadata.
Example:
var request = “<Request xsi:type=’RetrieveAllEntitiesRequest’>” +
“<MetadataItems>EntitiesOnly</MetadataItems>” +
“<RetrieveAsIfPublished>false</RetrieveAsIfPublished>” +
“</Request>”;
var result = gQueryMetadataService(request);
var logicalNames = result.selectNodes(“//CrmMetadata/LogicalName”);
*/
gQueryMetadataService = function(request) {
var xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
xmlhttp.open(“POST”, ‘/mscrmservices/2007/MetadataService.asmx’, false);
xmlhttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
xmlhttp.setRequestHeader(“SOAPAction”, ‘http://schemas.microsoft.com/crm/2007/WebServices/Execute&#8217;);
var soapMessage = “<?xml version=’1.0′ encoding=’utf-8′?>” + “<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance&#8217; ” + “xmlns:xsd=’http://www.w3.org/2001/XMLSchema&#8217; xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>&#8221; + GenerateAuthenticationHeader() + “<soap:Body><Execute xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>&#8221; + request+ “</Execute></soap:Body>” + “</soap:Envelope>”;
xmlhttp.send(soapMessage);
return xmlhttp.responseXML;
}

Advertisements

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

%d bloggers like this: