Skip to content
← All tips

How to run a FetchXML query with Xrm.WebApi

Once the filter gets complicated, with joins, link-entity or aggregates, I find it easier to copy the FetchXML from Advanced Find than to translate it into OData. retrieveMultipleRecords takes it as is:

const fetchXml = `
<fetch top="50">
  <entity name="account">
    <attribute name="name" />
    <filter>
      <condition attribute="address1_city" operator="eq" value="Madrid" />
    </filter>
  </entity>
</fetch>`;
 
const result = await Xrm.WebApi.retrieveMultipleRecords(
  "account",
  "?fetchXml=" + encodeURIComponent(fetchXml)
);
result.entities.forEach((a) => console.log(a.name));

The one thing you can’t skip is encodeURIComponent. Without it, characters like &, # or spaces break the URL, and the query either fails or returns something you didn’t expect. Also, the table name in the first parameter has to match the entity in the FetchXML.

For simple filters I still go with OData (?$select=...&$filter=...), which is shorter and easier to read.

Share:LinkedInX

Did it help?

If you need help with this in your project, write to me.

More tips