Calling the RetrieveEntity WebAPI Function with Enum Types

Leave a comment

In Dynamics 365, there are several functions that we can call from the Web API. You can see a list of these functions here. When calling these functions, we can use Xrm.WebApi.execute and specify an action as the operationType:

In some cases, this doesn’t appear to work as expected, and you may need to revert to sending through the request using XMLHttpRequest.

Let’s look at the example of using the function RetrieveEntity, which is part of the Microsoft.Dynamics.CRM namespace.

In this case, the first parameter is EntityFilters:

Which is actually an EntityFilters eNum type:

In order to send this through in the request, we are going to use the following notation. The syntax can be a little tricky:

Microsoft.Dynamics.CRM.EntityFilters’All’

Here’s the code:

[sourcecode language=”JavaScript”] var context = Xrm.Utility.getGlobalContext();
var requestUrl = context.getClientUrl() + "/api/data/v9.1/RetrieveEntity(EntityFilters=@p1,LogicalName=@p2,MetadataId=@p3,RetrieveAsIfPublished=@p4)";
requestUrl += "?@p1=" + encodeURIComponent("Microsoft.Dynamics.CRM.EntityFilters’All’");
requestUrl += "&@p2=’account’";
requestUrl += "&@p3=" + encodeURIComponent("70816501-edb9-4740-a16c-6a5efbc05d84");
requestUrl += "&@p4=false";

var req = new XMLHttpRequest();
req.open("GET", requestUrl, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;

if (this.status === 200) {
var results = JSON.parse(this.response);
debugger;
} else {
var errorText = this.responseText;
debugger;
}
}
};
req.send();

[/sourcecode]

Note the 2nd parameter is the MetadataId of the Entity, which you can get using this query:

/api/data/v9.0/EntityDefinitions?$filter=LogicalName%20eq%20’account’&$select=MetadataId

Running this in the console, we get our entity metadata returned:

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

Leave a Reply

Your email address will not be published. Required fields are marked *