Liquid script to fetch Contact names – Dynamics Portals SaaS
In Dynamics portals (SaaS) to make CRM server calls to retrieve the data we must rely on Liquid Script.
In this article, I am going to retrieve Contacts and loop through to capture Contact ‘Full Name‘ using Liquid Script and display on a Web Template.
Liquid script to fetch Contacts:
{% fetchxml settings %}
<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
<entity name=”contact”>
<attribute name=”fullname” />
<attribute name=”telephone1″ />
<order attribute=”fullname” descending=”false” />
</entity>
</fetch>
{% endfetchxml %}
{% assign result = settings.results.entities %}
{% for item in result %}
{% assign Names = {{item.fullname}} | append: ‘ , ‘ | append: {{Names}} %}
{% endfor %}// Set the Concatenated text to Span so that it can be read in JScript
<span id=”spanContactNames” style=”display:block”>{{Names}}</span>
Complete Web Template Source:
<html lang=”en” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta charset=”utf-8″ />function onload() {
// Read Contact names from Span
var spanContactNames = document.getElementById(“spanContactNames”);
if (spanContactNames) {
alert(spanContactNames.innerText);
}
}</head>
<body onload=”onload()”>
<h1>**Contact Names**</h1>
{% fetchxml settings %}
<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
<entity name=”contact”>
<attribute name=”fullname” />
<attribute name=”telephone1″ />
<order attribute=”fullname” descending=”false” />
</entity>
</fetch>
{% endfetchxml %}
{% assign result = settings.results.entities %}
{% for item in result %}
{% assign Names = {{item.fullname}} | append: ‘ , ‘ | append: {{Names}} %}
{% endfor %}
<span id=”spanContactNames” style=”display:block”>{{Names}}</span>
</body>
</html>
Note :
- ‘Liquid Script’ gets executed during the Web Page render and hence the fetched values will be available on Page ‘onload’ script event.
- ‘Liquid Script’ will respect the Portal security model (i.e., It fetches the records only if you have access granted by your Web Role)
How to test:
- Create a new Web Template and paste the content to “Source” field.
- Add a Web Link and map your Web Template.
🙂