{"id":15464,"date":"2019-04-18T12:59:31","date_gmt":"2019-04-18T12:59:31","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=15464"},"modified":"2019-05-04T12:02:01","modified_gmt":"2019-05-04T12:02:01","slug":"handling-special-characters-while-executing-fetch-xml-programmatically-in-web-api-rest-call","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/04\/handling-special-characters-while-executing-fetch-xml-programmatically-in-web-api-rest-call\/","title":{"rendered":"Handling special characters while executing fetch XML programmatically and in Web API rest call"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>We frequently use <strong>Fetch<\/strong> 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.<\/p>\n<p>Also, if we add Fetch XML in <strong>Web API rest call<\/strong>, then at times we do not get result due to few special characters added in fetch XML.<\/p>\n<p>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.<\/p>\n<p>Now, first let\u2019s look into how the fetch behaves differently when adding some special symbols:<\/p>\n<p>As we all know fetch XML is in XML format which follows below sample structure.<\/p>\n<p>&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;false&#8221;&gt;<\/p>\n<p>&lt;entity name=&#8221;account&#8221;&gt;<\/p>\n<p>&lt;attribute name=&#8221;name&#8221; \/&gt;<\/p>\n<p>&lt;attribute name=&#8221;accountid&#8221; \/&gt;<\/p>\n<p>&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;<\/p>\n<p>&lt;filter type=&#8221;and&#8221;&gt;<\/p>\n<p>&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; <strong>value=&#8221;test&#8221;<\/strong> \/&gt;<\/p>\n<p>&lt;\/filter&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p>This fetch works both times, whether we execute fetch XML programmatically or in Web API rest call.<\/p>\n<p>Here, many times we get requirements where we need to add value in the condition tag programmatically, for example instead of value=\u201dtest\u201d we add value=\u201dTest Company A &amp; B\u201d in condition tag programmatically for our test account record.<\/p>\n<p>Now, the above fetch is changed to &#8211;<\/p>\n<p>&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;false&#8221;&gt;<\/p>\n<p>&lt;entity name=&#8221;account&#8221;&gt;<\/p>\n<p>&lt;attribute name=&#8221;name&#8221; \/&gt;<\/p>\n<p>&lt;attribute name=&#8221;accountid&#8221; \/&gt;<\/p>\n<p>&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;<\/p>\n<p>&lt;filter type=&#8221;and&#8221;&gt;<\/p>\n<p>&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; <strong>value=&#8221;Test Company A &amp; B&#8221;<\/strong> \/&gt;<\/p>\n<p>&lt;\/filter&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p>But if we execute this fetch XML programmatically, it doesn\u2019t give us any result but if the same fetch is executed manually then it gives us result.<\/p>\n<p>The reason behind this is the special character \u201c&amp;.\u201d As the fetch converts <strong>&amp;<\/strong>, <strong>\u201d<\/strong>, <strong>&gt;<\/strong> and <strong>&lt;<\/strong> characters to <strong>&amp;amp;<\/strong> , <strong>&amp;quot;<\/strong> , <strong>&amp;gt;<\/strong> and <strong>&amp;lt;<\/strong>.<\/p>\n<p>So instead of \u201cTest Company A &amp; B\u201d, the value should be converted and used as to \u201cTest Company A &amp;amp; B\u201d.<\/p>\n<p>The final working Fetch XML becomes as below:<\/p>\n<p>&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;false&#8221;&gt;<\/p>\n<p>&lt;entity name=&#8221;account&#8221;&gt;<\/p>\n<p>&lt;attribute name=&#8221;name&#8221; \/&gt;<\/p>\n<p>&lt;attribute name=&#8221;accountid&#8221; \/&gt;<\/p>\n<p>&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;<\/p>\n<p>&lt;filter type=&#8221;and&#8221;&gt;<\/p>\n<p>&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; <strong>value=&#8221;Test Company A &amp;amp; B&#8221;<\/strong> \/&gt;<\/p>\n<p>&lt;\/filter&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p>Now, let\u2019s look at how we can handle special characters of fetch XML while adding in Web API rest call:<\/p>\n<p>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.<\/p>\n<p>https:\/\/yourcrmname.api.crm.dynamics.com\/api\/data\/v9.1\/accounts?fetchXml=&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;false&#8221;&gt;&lt;entity name=&#8221;account&#8221;&gt;&lt;attribute name=&#8221;name&#8221; \/&gt;&lt;attribute name=&#8221;accountid&#8221; \/&gt;&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;&lt;filter type=&#8221;and&#8221;&gt;&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; <strong>value=&#8221;Test Company A &#8220;<\/strong> \/&gt;&lt;\/filter&gt;&lt;\/entity&gt;&lt;\/fetch&gt;<\/p>\n<p>But if we have special symbol in value as in above fetch XML (for example, value=&#8221;Test Company A &amp; B&#8221;) without converting special characters in values, then it gives error. This is since \u2018&amp;\u2019 character in rest call is considers as start of next URL parameter.<\/p>\n<p>So first we need to convert fetch XML to convert special characters in values as mentioned above. This will add <strong>&amp;amp;<\/strong> , <strong>&amp;quot;<\/strong> , <strong>&amp;gt;<\/strong> or <strong>&amp;lt; <\/strong>if there are <strong>&amp;<\/strong>, <strong>\u201d<\/strong>, <strong>&gt;<\/strong> or <strong>&lt; <\/strong>characters in fetch XML. If we still execute below xml we will get error<\/p>\n<p>https:\/\/yourcrmname.api.crm.dynamics.com\/api\/data\/v9.1\/accounts?fetchXml=&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;false&#8221;&gt;&lt;entity name=&#8221;account&#8221;&gt;&lt;attribute name=&#8221;name&#8221; \/&gt;&lt;attribute name=&#8221;accountid&#8221; \/&gt;&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;&lt;filter type=&#8221;and&#8221;&gt;&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; <strong>value=&#8221;Test Company A &amp;amp; B&#8221;<\/strong> \/&gt;&lt;\/filter&gt;&lt;\/entity&gt;&lt;\/fetch&gt;<\/p>\n<p>Now, in script we need to use <strong>encodeURIComponent<\/strong> to convert the above created Fetch XML, so that \u2018&amp;\u2019 character will be converted in its representative characters. The resulting working rest call will be as shown below:<\/p>\n<p>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%20<strong>value%3D%22Test%20Company%20A%20%26amp%3B%20B<\/strong>%22%20%2F%3E%3C%2Ffilter%3E%3C%2Fentity%3E%3C%2Ffetch%3E<\/p>\n<h2>Conclusion<\/h2>\n<p>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.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/integrations\/inolink-quickbooks-microsoft-dynamics-crm\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-15467\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/Get-started-with-QuickBooks-and-Dynamics-365-Integration.png\" alt=\"\" width=\"820\" height=\"205\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/04\/handling-special-characters-while-executing-fetch-xml-programmatically-in-web-api-rest-call\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":18201,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[65],"tags":[],"class_list":["post-15464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-webapi"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=15464"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15464\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/18201"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=15464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=15464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=15464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}