How to use htmlEncode functionality in Dynamics 365 CRM

By | April 3, 2019

Introduction

Recently we were facing an issue where the presence of special character in string hindered the search of record in Dynamic CRM web and UCI. After working on this issue it was found that there are some more special characters which does not work like:  & “ ‘  < > + ; etc.

Even encoding that string by encodeURIComponent and adding it to FetchXml resulted in display of errors in web and UCI, such as:

  • Invalid Xml.
  • URIError: URI malformed
  • http Error 500

To resolve this issue in Dynamic CRM v9, we finally made use of htmlEncode.

Xrm.Encoding.htmlEncode(arg)

Pass the record name in htmlEncode and use the resultant string to search record. There will be no further hindrance in finding the respective record in Dynamics CRM.

Fetchxml without htmlEncode:

<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>

<entity name=’new_projectdetails’>

<all-attributes /><filter type=’and’>

<condition attribute=’ikl_name’ operator=’eq’ value=’Ben & Jerry’s Leads’ />

<condition attribute=’statecode’ operator=’eq’ value=’0’/>

</filter>

</entity>

</fetch>

FetchXml with htmlEncode:

<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>

<entity name=’new_projectdetails’>

<all-attributes /><filter type=’and’>

<condition attribute=’ikl_name’ operator=’eq’ value=’Ben &#38; Jerry&#39;s Leads’ />

<condition attribute=’statecode’ operator=’eq’ value=’0’/>

</filter>

</entity>

</fetch>

But in dynamics CRM 8.2 we can’t use htmlEncode to encode the string. To make this possible we have to write one function which converts the special character present in string.

This function in htmlEncode will convert the special character into the Html Number. Html Number is combination of &# and decimal number of that character and ;

For example:

For & special character decimal number is 38, so htmlEncode for & is &#38;

So to convert the special character to html number, we have to write the below function:

function handleSpecialChar(string) {

try {

return string.replace(/[\x26\x2B\x3B\<>'”]/g, function (r) { return “&#” + r.charCodeAt(0) + “;” })

} catch (e) {

showAlert(“handleSpecialChar Error: ” + e || e.message || e.description);

}

}

Use the above function to encode the special character in string.

For example: Ben & Jerry’s Lead

String htmlEncode is:   ‘Ben &#38; Jerry&#39;s Leads

Now pass the resultant string in FetchXml to search record in Dynamics CRM.

Conclusion

In the presence of special characters in string you can use htmlEncode to search records in Dynamics 365 CRM.

Attach2Dynamics

 

One thought on “How to use htmlEncode functionality in Dynamics 365 CRM

Comments are closed.