Query Web API using FetchXML


FetchXML will be very useful to use with Web API for complex queries as I have encountered and issue where we can’t make grouping conditions using the normal ODATA style filter, Also a lot of operators provided by fetchXML is not provided by the normal ODATA Queries example: In , Today , Next X Days, etc.

So to call fetchXML using JavaScript and XRM.WebApi please follow these simple steps:

Construct your fetchXML using Advanced find and then download FetchXML.

var fetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
                        '<entity name="account">' +
                        '<attribute name="accountid" />' +
                        '<order attribute="fullname" descending="false" />' +
                        '<filter type="and">' +
                        '<condition attribute="statecode" operator="eq"  value="0" />' +
                        '</filter>' +
                        '</entity>' +

Prefix you fetchXML variable with the word “?fetchXml” and the concatenate the encodeURIComponent(fetchXML)

 fetchXML = "?fetchXml="+encodeURIComponent(fetchXML);

Call retrieveMultipleRecords as below:

Xrm.WebApi.online.retrieveMultipleRecords("account", fetchXML).then(
                        function success(results) {
                            if (results.entities.length > 0) {
                                var accountid = results.entities[i]["accountid"];
                        function (error) {

Hope it is useful!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: