DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN C#

Previously, we saw how to execute FetchXml query using Xrm.WebApi in JavaScript

In this quick post, we will see how to execute FetchXml query in C#.

  1. Prepare the FetchXml you want to execute whether from the Advanced Find, or any other FetchXml generator in the community
  2. Create the C# function you want to call in order to execute the retrieve request and Copy/Paste the FetchXml query
  3. In order to be able to execute FetchXml queries in C#, you must use the FetchExpression class instead of QueryExpression as per the below

    public void FetchXmlInFetchExpression()
    {
    var fetchQuery = @"
    <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='account'>
    <attribute name='name' />
    <attribute name='primarycontactid' />
    <attribute name='telephone1' />
    <attribute name='accountid' />
    <order attribute='name' descending='false' />
    <filter type='and'>
    <condition attribute='primarycontactid' operator='not-null' />
    <condition attribute='name' operator='like' value='%Northwind%' />
    </filter>
    </entity>
    </fetch>";
    var objRecords = AdminService.RetrieveMultiple(new FetchExpression(fetchQuery));
    }

  4. The above fetchXml is a very simple query, but you can build and execute more complex queries based on your needs


Hope This Helps!

Comments

Popular posts from this blog

DYNAMICS 365 FILTER LOOKUP FIELD BASED ON N:N RELATIONSHIP

DYNAMICS 365 LEVEL UP BROWSER EXTENSION - PART 1 - FORMS

DYNAMICS 365 REFERENCE PANEL