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:

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();

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:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

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