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:

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

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:

 

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

 

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 *