Convert FetchXML to QueryExpression in Dynamics 365 with Web API Example

1 Comment

In this post, we will go through an example of converting FetchXML to a QueryExpression. We will do this by debugging a Web API call in a plugin.

To debug Web API calls using the Plugin Registration Tool, do the following.

First, create a Web API query, e.g. select the name and revenue fields in the accounts entity filtered by name. E.g. https://yourorg.api.crm.dynamics.com/api/data/v9.1/accounts?$select=name,revenue&$filter=name eq ‘3M’

In Visual Studio, create a new Class Library:

Click Next

Add NuGet packages:

Use the following code. We will use FetchXmlToQueryExpressionRequest and FetchXmlToQueryExpressionResponse to convert FetchXML to a QueryExpression:

[sourcecode language=”CSharp”] using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;

namespace Carl.PluginWebAPI
{
public class PluginWebAPI : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

if (context.InputParameters["Query"] is FetchExpression)
{
FetchExpression fetchExpression = context.InputParameters["Query"] as FetchExpression;
FetchXmlToQueryExpressionRequest fetchXmlToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest()
{
FetchXml = fetchExpression.Query
};
FetchXmlToQueryExpressionResponse fetchXmlToQueryExpressionResponse = (service.Execute(fetchXmlToQueryExpressionRequest) as FetchXmlToQueryExpressionResponse);
}
}
}
}
[/sourcecode]

Sign the assembly:

Register the assembly:

Select the assembly:

Register the assembly:

Click OK:

Register New Step:

Select account:

Click Install Profiler:

Start Profiling:

Click OK:

Refresh the Web API web page from the first step, then stop profiling once complete.

Now attach the Visual Studio project to the Plugin Registration Tool process:

Select PluginRegistration.exe:

In the plugin registration tool, click Debug:

Next to Profile, select the down arrow:

Choose the profile that just ran for this entity:

Choose the assembly for the project and click Start Execution:

We can see the FetchXML query:

And then the converted Query Expression with the columns and filters:

 

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

One Response to Convert FetchXML to QueryExpression in Dynamics 365 with Web API Example

  1. install package nuget –> Microsoft.Dynamics.Sdk.Messages
    require
    using Microsoft.Crm.Sdk.Messages

    var fechtXML2 = ((QueryExpressionToFetchXmlResponse)service.Execute( (new QueryExpressionToFetchXmlRequest { Query = query }))).FetchXml;

Leave a Reply

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