In part 1 of this post I demonstrated building and using a service which receives a FetchXML query and returns Dynamics 365 data to any Portal page in an asynchronous manner as a JSON object. This service is similar to the SDK’s RetrieveMultiple message.
In this part 2, I’ll demonstrate a different service, which like the SDK Retrieve message, receives a record type, record id (GUID) and columns set to return the required data as a JSON object.
This is useful when you already have a specific Dynamics 365 record id at hand and you want to retrieve additional data for this record.
- Create a Web Template named RetrieveService
Set the Mime Type is to application/json.
Paste the following code in to the web template source and save.
{% comment %} test for required parameters {% endcomment %}
{% if request.params[‘entityname’] and request.params[‘id’] and request.params[‘columnset’] %}{% capture msg_no_data %}No data for specified attribute {% endcapture %}
{% comment %} extract parameters {% endcomment %}
{% comment %} extract target entity {% endcomment %}
{% assign entityname = request.params[‘entityname’] %}
{% comment %} extract target record id {% endcomment %}
{% assign id = request.params[‘id’] %}
{% comment %} parse requested attributes into array {% endcomment %}
{% assign columnset = request.params[‘columnset’] | split: “,” %}{% comment %} query for target entity {% endcomment %}
{% assign item = entities[request.params[‘entityname’]][request.params[‘id’]] %}{% comment %} Emit JSON response {% endcomment %}
{% if item %}{
{% comment %} Iterate throguh requestd attributes array {% endcomment %}
{% for att in columnset %}
{% comment %} Handle optionset attribute {% endcomment %}
{% if item[att].label %}”{{ att }}”:”{{ item[att].label | default: msg_no_data }}”
{% comment %} Handle lookup attribute {% endcomment %}
{% elseif item[att].name %}”{{ att }}”:{“name”:”{{ item[att].name | default: msg_no_data }}”,”id”:”{{ item[att].id | default: msg_no_data }}”}
{% comment %} Handle other attributes {% endcomment %}
{% else %}”{{ att }}”:”{{ item[att] | default: msg_no_data }}”
{% endif %}{% unless forloop.last %},{% endunless %}
{% endfor -%}
}
{% endif %}{% comment %} handle missing parameters {% endcomment %}
{% else %}
{ “error”:”Missing one or more required parameter(s): entityname, id, columnset” }
{% endif %} - Create a Page Template named RetrieveService
Map the Page Template to the Web Template created on step 1 above.
Uncheck the Header and Footer checkbox.- Create a Portal Page named RetrieveService
Set Home as parent page.
Map the page to the Page Template created on step 2 and copy the partial URL which is used in the next step- Consume Service
Use the following code sample anywhere you need asynchronous data retrieval of from Dynamics 365. In my example it is located in the Home page JavaScript portion.
Note the URL address which maps to the Page (created above) URL.
Replace the entityname, id and columnset parameters.
Most important: make sure your users have the required Entity Permissions for the entities queried, otherwise they will receive empty results.$(function (){
//define asynchronous request from any portal page
$.ajax({
method: “POST”,
url: “/retrieveservice/”,
data: {
//define entity name, id and columnset
entityname: ‘incident‘,
id: ‘D2A697CD-D3C7-E811-A965-000D3AB0F1D7‘,
columnset: ‘ticketnumber,title,createdon,incidentid,statecode,caseorigincode,customerid,ownerid‘
}
}).done(function (msg){
console.log(msg);
}).fail(function (jqXHR, textStatus, errorThrown){
console.log(textStatus);
});
});As the sample code logs the resulting JSON object to the console, open the browser developers tool (F12) to view it.
- Create a Portal Page named RetrieveService