web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

DYNAMICS 365 HOW TO EXECUTE FETCHXML QUERIES IN C#

Charles Abi Khirs Profile Picture Charles Abi Khirs 3,569

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!

This was originally posted here.

Comments

*This post is locked for comments