Skip to main content
Community site session details

Community site session details

Session Id : Jvo9ZRacDAnaNyymnPdL6A
Power Pages - Power Apps Portals
Unanswered

Using AJAX Request with JSON

Like (0) ShareShare
ReportReport
Posted on 12 Apr 2021 11:43:48 by

I am trying to retrieve a record's data from Dynamics CRM in portals using AJAX request and JSON. Although i have the issue where the resultset of the GET action for AJAX is not getting stored in the variable(json). below is the code which fetches the request. This calls another web template where the JSON setup is done and the fetch query is implemented.

 

 

$(document).ready(function(){

$('#UpdateButton').after('<button id="searchResources" type="button" class="btn btn-primary button" style="margin-left:10px;"><span></span> Search Resources</button>');

$("#searchResources").click(function(){
try {
getAvailableResources();
} catch (e) {
console.log("Search Resources :: " + e.message);
}
});
});

function getAvailableResources(){
debugger;
var webTemplateURL = "";
var workorderid = "";
var workOrderCollection = null;
try {
workorderid = $("#msdyn_name").val();
alert(workorderid);
webTemplateURL = "https://sampleportalurs.powerappsportals.com/table-of-contents/page/getworkorderjson/?workorderid=" + workorderid + "";
workOrderCollection = getResponse(webTemplateURL);
if (workOrderCollection.results.length > 0) {
workOrderCollection.results.forEach(function (workorder) {
alert(workorder.name);
});
}
} catch (e) {
console.log("getAvailableResources :: " + e.message);
}
}
//Function to execute ajax request

function getResponse(webTemplateURL) {
var response = null;
try {
$.ajax({type: "GET", url: webTemplateURL, dataType: 'json', async: false, success: function(json){
response = json;
alert("test"+json);
alert("test"+response);
}});
} catch (e) {
console.log("getResponse :: " + e.message);
}
return response;
}

function isValid(attributes) {
try {
if (attributes != null&& attributes != undefined && attributes != "") {
return true;
}
else {
return false;
}
} catch (e) {
console.log("isValid :: " + e.message);
}
}

 

 

The Web Template Code:

 

 

{% fetchxml workorder %}

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="msdyn_workorder">
<attribute name="createdon" />
<attribute name="msdyn_serviceaccount" />
<attribute name="msdyn_workorderid" />
<attribute name="msdyn_functionallocation" />
<attribute name="msdyn_timetopromised" />
<attribute name="msdyn_timefrompromised" />
<attribute name="msdyn_primaryincidentestimatedduration" />
<order attribute="msdyn_name" descending="false" />
<filter type="and">
<condition attribute="msdyn_name" operator="eq" value='{{request.params["workorderid"]}}' />
</filter>
</entity>
</fetch>

{% endfetchxml %}

{% if workorder.results.entities.size > 0 %}

{

"results":

[
{% for wo in workorder.results.entities %}

{

"name": "{{wo.msdyn_name}}",

"serviceaccount": "{{wo.msdyn_serviceaccount}}"

"workorderid": "{{wo.msdyn_workorderid}}"

"timetopromised": "{{wo.msdyn_timetopromised}}"

"timefrompromised": "{{wo.msdyn_timefrompromised}}"

"estimatedduration": "{{wo.msdyn_primaryincidentestimatedduration}}"
}

{% unless forloop.last %},{% endunless %}

{% endfor %}

]

}

{% else %}

{% if page.adx_entityform %}
{% entityform id: page.adx_entityform.id %}
{% endif %}

{% endif %}

 

 

while i execute this https://sampleportalurs.powerappsportals.com/table-of-contents/page/getworkorderjson/?workorderid=00001, the result is as the attached image. I believe it is  fetching the record data fromthe JSON. Although on click of the button which inturn uses the ajax request, the results are not getting retrieved. Kindly help me with this issue.issuewith ajax.PNG

  • Christian Leverenz Profile Picture
    1,214 on 05 May 2021 at 16:22:06
    Re: Using AJAX Request with JSON

    Hi Mahesh_Mohan_,

    You should uncheck the "show website header and footer" on the pagetemplate for the page carrying your service. This will then just render your jsontemplate and not all the other stuff.

    It's a little bit a misleading name because it not only suppresses header and footer, the liquid only is rendered 🙂

     

    Additionally i would highly recommend to dedicate one liquid fetch template for the service. So do NOT respond with something like 

    {% else %}

    {% if page.adx_entityform %}
    {% entityform id: page.adx_entityform.id %}
    {% endif %}

    only because you did not find any workorders. Just return an empty json (or null, which i do not recommend 🙂 )

     

    Hope this helps if you haven't found out yet,

      Christian

     

    PS bonuspoints if you set the content type of the liquid to application/json 🙂

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
Lucas001 Profile Picture

Lucas001 60 Super User 2025 Season 2

#2
Fubar Profile Picture

Fubar 55 Super User 2025 Season 2

#3
surya narayanan Profile Picture

surya narayanan 35

Loading complete