In our last article we discussed new enhancement in Web API for create and update request. In this article we are going to discuss enhancement released to query metadata. Web API made it really simple to query your entity metadata now. If we need to query any entity metadata we can just specific entity logical name in our Web API get request like following:
api/data/v8.2/EntityDefinitions(LogicalName='lead')
Here is the complete request for query lead entity metadata:
var serverURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest(); req.open("GET", serverURL + "/api/data/v8.2/EntityDefinitions(LogicalName='lead')", true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function() { if (this.readyState == 4 /* complete */ ) { req.onreadystatechange = null; if (this.status == 200) { var data = JSON.parse(this.response); //process metadata } else { var error = JSON.parse(this.response).error; alert(error.message); } } }; req.send();
It will return lead entity metadata like following:
If we want to return all the entity metadata we can use our request like following:
api/data/v8.2/EntityDefinitions
We can also use other variations while querying metadata for example if we want to get all attributes metadata of lead entity, we can use like following
api/data/v8.2/EntityDefinitions(LogicalName='lead')/Attributes
And if want to retrieve only specific attribute, we can use following request:
api/data/v8.2/EntityDefinitions(LogicalName='lead')/Attributes(LogicalName='address1_city')
We can also query global option set metadata using following request:
api/data/v8.2/GlobalOptionSetDefinitions(Name='incident_caseorigincode')
and we will get response like following:
Similarly we can also query relationship using:
api/data/v8.2/RelationshipDefinitions(SchemaName='account_originating_lead')
Stay tuned for more Dynamics 365 features !!
Pingback: Dynamics 365 Web API enhancement Part 2 - Microsoft Dynamics CRM Community