Introduction

We frequently use Fetch to search records in CRM and few times use the Fetch XML in code for ease of changing fetch query, if needed. Fetch XML also supports adding most of the special symbols. Due to this, most of the times we do not go in depth to check how fetch behaves when adding certain special characters.

Also, if we add Fetch XML in Web API rest call, then at times we do not get result due to few special characters added in fetch XML.

So, in this blog, we are mainly looking at how fetch behaves differently when adding some special symbols. We will also look at how we can handle special characters of fetch XML while adding in Web API rest call.

Now, first let’s look into how the fetch behaves differently when adding some special symbols:

As we all know fetch XML is in XML format which follows below sample structure.

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”account”>

<attribute name=”name” />

<attribute name=”accountid” />

<order attribute=”name” descending=”false” />

<filter type=”and”>

<condition attribute=”name” operator=”eq” value=”test” />

</filter>

</entity>

</fetch>

This fetch works both times, whether we execute fetch XML programmatically or in Web API rest call.

Here, many times we get requirements where we need to add value in the condition tag programmatically, for example instead of value=”test” we add value=”Test Company A & B” in condition tag programmatically for our test account record.

Now, the above fetch is changed to –

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”account”>

<attribute name=”name” />

<attribute name=”accountid” />

<order attribute=”name” descending=”false” />

<filter type=”and”>

<condition attribute=”name” operator=”eq” value=”Test Company A & B” />

</filter>

</entity>

</fetch>

But if we execute this fetch XML programmatically, it doesn’t give us any result but if the same fetch is executed manually then it gives us result.

The reason behind this is the special character “&.” As the fetch converts &, , > and < characters to &amp; , &quot; , &gt; and &lt;.

So instead of “Test Company A & B”, the value should be converted and used as to “Test Company A &amp; B”.

The final working Fetch XML becomes as below:

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>

<entity name=”account”>

<attribute name=”name” />

<attribute name=”accountid” />

<order attribute=”name” descending=”false” />

<filter type=”and”>

<condition attribute=”name” operator=”eq” value=”Test Company A &amp; B” />

</filter>

</entity>

</fetch>

Now, let’s look at how we can handle special characters of fetch XML while adding in Web API rest call:

While running Fetch XML through Web API rest call, if we use fetch XML without any special symbol in value as shown below, then we get correct result without any error.

https://yourcrmname.api.crm.dynamics.com/api/data/v9.1/accounts?fetchXml=<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”><entity name=”account”><attribute name=”name” /><attribute name=”accountid” /><order attribute=”name” descending=”false” /><filter type=”and”><condition attribute=”name” operator=”eq” value=”Test Company A “ /></filter></entity></fetch>

But if we have special symbol in value as in above fetch XML (for example, value=”Test Company A & B”) without converting special characters in values, then it gives error. This is since ‘&’ character in rest call is considers as start of next URL parameter.

So first we need to convert fetch XML to convert special characters in values as mentioned above. This will add &amp; , &quot; , &gt; or &lt; if there are &, , > or < characters in fetch XML. If we still execute below xml we will get error

https://yourcrmname.api.crm.dynamics.com/api/data/v9.1/accounts?fetchXml=<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”><entity name=”account”><attribute name=”name” /><attribute name=”accountid” /><order attribute=”name” descending=”false” /><filter type=”and”><condition attribute=”name” operator=”eq” value=”Test Company A &amp; B” /></filter></entity></fetch>

Now, in script we need to use encodeURIComponent to convert the above created Fetch XML, so that ‘&’ character will be converted in its representative characters. The resulting working rest call will be as shown below:

https://yourcrmname.api.crm.dynamics.com/api/data/v9.1/accounts?fetchXml=%3Cfetch%20version%3D%221.0%22%20output-format%3D%22xml-platform%22%20mapping%3D%22logical%22%20distinct%3D%22false%22%3E%3Centity%20name%3D%22account%22%3E%3Cattribute%20name%3D%22name%22%20%2F%3E%3Cattribute%20name%3D%22accountid%22%20%2F%3E%3Corder%20attribute%3D%22name%22%20descending%3D%22false%22%20%2F%3E%3Cfilter%20type%3D%22and%22%3E%3Ccondition%20attribute%3D%22name%22%20operator%3D%22eq%22%20value%3D%22Test%20Company%20A%20%26amp%3B%20B%22%20%2F%3E%3C%2Ffilter%3E%3C%2Fentity%3E%3C%2Ffetch%3E

Conclusion

While modifying and executing fetch XML programmatically, we should always handle special characters as mentioned above. We should also handle special characters and then encode fetch XML while using it in Web API rest call.