In my previous blog(Get Start with writing Liquid templates in Dynamics PowerApps Portal), I have explained about the liquid code in detail and now I will provide some snippets or code which are used regularly while designing a page or creating a web-template.
How to check for the Logged-in User in Dynamics also check for User Role:
{% if user %}
//write steps if the user has logged in
{% else %}
// write steps if no user has signed in
//you can redirect to sign-in Page
{% endif %}
//Check for certain user role,
{% if user.roles contains 'Community User'%}
//Do required steps
{% endif %}
—
Assigning variables in Liquid template using GUID and URL.
{% assign account = entities.account[user.parentcustomerid.id] %}
//assign a variable using the page url
//powerappsportals.com/account-information/?id=317fa07b-c11*****
{% assign account= request.params['id'] %}
//assign the Lookup field value to a variable
{% assign LookupValueName = account.sas_lookupdield.name %}
{% assign LookupValueGuid = account.sas_lookupdield.id%}
—
How to include different Dynamics portal component like Web-form, Entity-form, entity-list and other entities in Web-Templates Dynamically.
//Include an entity-form using liquid-tags
//Pass the name of the entity-form.
{% entityform name:'Edit-contact record' %}
//Include a Web-form using liquid-tags
//Pass the name of the Web-form.
{% webform name:'Create-contact record' %}
//Include an entity-list using liquid-tags.
//Pass the name of the entity-list.
{% include 'entity_list' key:'Display Contact Lists' %}
//Include a web-template using liquid-tags.
//Pass the name of the web-template.
{% include 'page_header' %}
//Include an editable content snippet.
//Pass the name of the content snippet.
{% editable snippets "Add Account Card" %}
—
How to loop and get all the Web-links child Entity record of a Web-Link set.
{% for link in primary_nav.weblinks %}
{% if link.name == "Account-Details" %}
{% if link.display_page_child_links %}
{% assign sublinks = sitemap[link.url].children %}
{% else %}
{% assign sublinks = link.weblinks %}
{% endif %}
{% for sublink in sublinks %}{% endfor %}
{% endif %}
{% endfor %}
—
Retrieve records in Liquid Tag/Code/Templates Using FetchXML
{% fetchxml account %}
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="account">
<attribute name="new_programtype"/>
<attribute name="name"/>
<attribute name="new_arpid"/>
<attribute name="accountid"/>
<attribute name="address1_city"/>
<attribute name="new_salesdocumentid"/>
<order attribute="prm_name" descending="false"/>
<filter type="and">
<condition attribute="new_parentaccount" operator="eq" value="{{ user.parentcustomerid.id }}"/>
</filter>
</entity>
</fetch>
{% endfetchxml %}
{% for item in account.results.entities %}
//Play with your data here|
var aprEntityName =
{{ item.new_arpid.name }}; //Get Lookup name
var programTypeEntityLabel =
{{ item.new_programtype.label }}; //Get OptionSet Value
var address =
{{ item.address1_city }}; // Get String fieldVAlue
{% endfor %}
—
Retrieve data using OData Feed by creating an Entity-List in Liquid Tags.
You first need to create an entity-List for this then in the OData-Tab you need to provide the configuration values.
Add the “OData Entity Type Name” field and provide the “OData Entity Set Name” as per your wish and select the view from the option-set. Save the data and then hit the URL combining your portal URL and “/_odata/” https://abcportal.powerappsportals.com/_odata/.
Important note: The oData feed that is published is anonymous and does not have any authorization checks, therefore do not enable odata feeds for CRM data that is not suitable for anonymous portal access or non-authorized Users.
Here are the screen-shots which will help you in creating the entity-list


Now hit the URL and this will show you the data you have exposed from the entity view.

Now include your odataSet name in the URl to get the exact no of records from the view

To acces the data from the web-Template you can use this code Syntax.
var xmlhttp = new XMLHttpRequest();
var url = "/_odata/blogpostSet";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonResults = JSON.parse(this.responseText);
var results = jsonResults.value;
for (var i = 0; i < results.length; i++) {
var Value = results[i].name;
}
}
};
That’s all for now, I hope this blog will help other portal developer.
Comment back for any suggestion or Queries.
Thank you all 🙂
Read my other post on Portal and liquid Templates:
Get Start with writing Liquid templates in Dynamics PowerApps Portal
Create a new Portal-Page in Dynamics PowerApps Portal
Dynamics Portal Entity-Structure and Architecture