How To Query The Entities In Dynamics CRM From ADX Portal Using JQuery

Introduction

This article demonstrates how to query the entities in Dynamics CRM using jQuery call and OData feeds from ADX Portal.

Steps to get records from Dynamics CRM entities

It is difficult to get the Dynamics CRM Entities from ADX Portal using any Web API call because that may require authorization token, client id, and client secret etc. Also, you will not be able to get any client context or to use XRMServiceToolKit.

ADX Portal user can be just a contact in Dynamics CRM and would not be a user in CRM domain. In this scenario, he will not have access to Dynamics CRM entities directly. He will be able to see the entities that are published in the ADX portal.

Here, in the below example, the user registers himself with the invitation code created in Dynamics CRM for Portal User having non-CRM domain email address (ex: Yahoo / Gmail / Hotmail).

Step 1

Create a contact in Dynamics CRM with the external email address and then generate the invitation code for that contact.



Step 2

With that Invitation code, the new user can be registered in the ADX portal.


Step 3

Select the Dynamics CRM entities that you want to query from ADX Portal. Go to the Portals in Dynamics CRM and then select the Entity list to add entities that are to be published to ADX Portal as OData Feeds.



Step 4

Also, enable Entity Permissions to restrict the OData Feeds which can be viewed by a logged in Portal User.

Step 5

Now, go to the Custom JavaScript section in Entity list form or Web Page (Portals) use the AJAX call for querying the OData feeds, as shown below.

  1. $(document).ready(function() {  
  2.     var portaluser = '{{user.fullname}}';  
  3.     var filteroption = "fullname eq '" + portaluser + "'";  
  4.     var odataUri = "https://<Portal URL>/_odata/Contacts";  
  5.     odataUri += "?$filter=" + encodeURIComponent(filteroption);  
  6.     // ajax call to OData feeds of Contacts entity with the filter for finding current logged in user’s record  
  7.     $.ajax({  
  8.         type: 'GET',  
  9.         contentType: 'application/json; charset=utf-8',  
  10.         datatype: 'json',  
  11.         url: odataUri,  
  12.         beforeSend: function(XMLHttpRequest) {  
  13.             XMLHttpRequest.setRequestHeader('Accept''application/json');  
  14.         },  
  15.         async: true,  
  16.         success: function(data, textStatus, xhr) {  
  17.             // Getting the company (Account) name that he belongs to  
  18.             var companyname = data.value[0].parentcustomerid.Name;  
  19.             $(".entitylist.entity-grid").on("loaded"function() {  
  20.                 $(this).children(".view-grid").find("td[data-attribute='new_name']").each(function(i, e) {  
  21.                     var attrvalue = $(this);  
  22.                     // To remove other company’s records from the custom entity list view  
  23.                     if (!(attrvalue.context.innerText.contains(companyname))) {  
  24.                         $(this).closest('tr').remove();  
  25.                     }  
  26.                 });  
  27.             });  
  28.         },  
  29.         error: function(xhr, textStatus, errorThrown) {  
  30.             alert(textStatus + ' ' + errorThrown);  
  31.         }  
  32.     });  
  33. });  

Output - Measure Details View in CRM Portal 

The output displays only the records that belong to the account the user is linked with.



Summary

In this article, I discussed how we can publish the entity as OData feeds and make use of AJAX call to find records from the OData feeds. Hope it will be useful for you all. Very soon, I will be back with a new article.