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:
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:


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

install package nuget –> Microsoft.Dynamics.Sdk.Messages
require
using Microsoft.Crm.Sdk.Messages
var fechtXML2 = ((QueryExpressionToFetchXmlResponse)service.Execute( (new QueryExpressionToFetchXmlRequest { Query = query }))).FetchXml;