Using encodeURIComponent when using ODATA queries in JavaScript and Dynamics CRM 2011

This week I ran into an issue that took me a while to figure related to the ODATA queries in Dynamics CRM. This might be a common mistake for other CRM and ODATA REST developers so I wanted to share it here.

Problem description

Here was the most difficult thing; there was not error at all and neither any error message. The JavaScript code that was put on an entity form customization just didn’t work as expected. It was not returning a record that we knew existed in the CRM system. The code looked similar to this

       var serverUrl = Xrm.Page.context.getServerUrl();
       var ODataPath = serverUrl + “/XRMServices/2011/OrganizationData.svc”;

       var contactReq = new XMLHttpRequest();
       contactReq.open(“Get”, ODataPath + “/ContactSet?$filter=EMailAddress1 eq ‘” + email + “‘ and StatusCode/Value eq 1”, false);
       contactReq.setRequestHeader(“Accept”, “application/json”);
       contactReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
       contactReq.onreadystatechange = function () {
                  contactEmailReqCallBack(this, changes, a, guid);
       };
       contactReq.send();

 

Basically this code requests a contact by looking at a specific email address that is passed to the parameter email. On my case the code was working for some emails and not for others, I realize the ones that were not working were like firstname+lastname@gmail.com for example. So the URL was formed as follows

http://crm2011/org/XRMServices/2011/OrganizationData.svc/ContactSet?$filter=EMailAddress1 eq ‘firstname+lastname@gmail.com‘ and StatusCode/Value eq 1

it didn’t return any record even though there was a contact in the CRM system with that email address. So, what was the problem here? The problem was the + sign is interpreted as space in the URI as well as other characters might be wrongly interpreted, like for example the / char.

The solution

Use the encodeURIComponent function to encode the email address properly in the URL. Our code changed now looks as follows:

       var serverUrl = Xrm.Page.context.getServerUrl();
       var ODataPath = serverUrl + “/XRMServices/2011/OrganizationData.svc”;

       var contactReq = new XMLHttpRequest();
       contactReq.open(“Get”, ODataPath + “/ContactSet?$filter=EMailAddress1 eq ‘” + encodeURIComponent(email) + “‘ and StatusCode/Value eq 1”, false);
       contactReq.setRequestHeader(“Accept”, “application/json”);
       contactReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);   

contactReq.onreadystatechange = function () {
    contactEmailReqCallBack(this, changes, a, guid);
};
contactReq.send();

Conclusion

Never forget to use the encodeURIComponent function when passing parameters on your ODATA queries!

I hope this helps others to ovoid a problem like the one descripted here.

For all who like to work with ODATA services in Dynamics CRM here is a great tool that will help creating the queries. http://crm2011odatatool.codeplex.com/

 

Advertisements