Apurv's D365 Blog

Code Samples & Troubleshooting Tips on Dynamics 365 (CRM) and Portals

D365 Portals : Using FetchXML with Liqiuid

Hello Everyone,

This is my first ever post with D365 Portals. I will bring up more interesting ones as we go along. Today I am going to talk about how you can customize your portal’s web template to use CRM fetch query with liquid.

Prerequisites:

  • FetchXML using Advanced Find in CRM
  • Customizing D365 Portals

Scenario:

D365 CRM provides an ability to categorize your articles using ISH (Interactive Service Hub). Those categories are used as tags and then displayed on D365 Portals as a list to browse the articles from. I will showcase how to modify the existing template and using FetchXML instead.

Build your FetchXML using Advanced Find

  • Go to Advanced Find
  • Select the entity as “Categories”
  • See FetchXML Sample

<fetch mapping=”logical” returntotalrecordcount=”true” count=”{{ count }}”>

<entity name=”category”>

<attribute name=”categorynumber” />

<attribute name=”title” />

<attribute name=”createdon” />

<attribute name=”categoryid” />

<order descending=”false” attribute=”title” />

</entity>

</fetch>

Review existing web template:

  • Go to D365 Online Instance – CRM
  • Go to Portals > Web Templates > Knowledge Base Home
  • Open the record
  • You will see the below code:

{% extends ‘layout_1_column’ %}
{% block main %}
{% include ‘Page Copy’ %}
{% assign category_url = sitemarkers[‘Category’].url %}
{% assign count = count | default: 0 %}
{% assign categories = knowledge.categories | top_level: count %}
{% if categories %}
<div class=”list-group unstyled”>
{% for category in categories %}
<a href=”{{ category_url | add_query: ‘id’, category.categorynumber }}” class=”list-group-item”>
{{ category.title }}
</a>
{% endfor %}
</div>
{% endif %}

Modify the Web Template:

  • Add the liqiuid objects in following format:

<!– New Category Display –>

{% extends ‘layout_1_column’ %}
<!– OLD Category Display –>
{% block main %}
{% include ‘Page Copy’ %}
{% assign category_url = sitemarkers[‘Category’].url %}
{% assign count = count | default: 0 %}
{% assign categories = knowledge.categories | top_level: count %}
{% if categories %}
<div class=”list-group unstyled”>
{% for category in categories %}
<!– <a href=”{{ category_url | add_query: ‘id’, category.categorynumber }}” class=”list-group-item”>
{{ category.title }}
</a> –>
{% endfor %}
</div>

{% endif %}
<!– OLD Category Display –>

<!– New Category Display –>
<div class=”list-group”>
{% fetchxml categories_query %}
<fetch mapping=”logical” returntotalrecordcount=”true” count=”{{ count }}”>
<entity name=”category”>
<attribute name=”categorynumber” />
<attribute name=”title” />
<attribute name=”createdon” />
<attribute name=”categoryid” />
<order descending=”false” attribute=”title” />
</entity>
</fetch>
{% endfetchxml %}
{% assign categories = categories_query.results.entities %}
{% if categories %}
<div class=”list-group”>
<h4 class=”list-group-item-heading”>Articles by Categories</h4> <br/> <br/>
{% for category in categories %}
<a class=”list-group-item” href=”{{ category_url | add_query: ‘id’, category.categorynumber }}{{ category.categorynumber | escape }}”>{{ category.title | escape }}</a>
{% endfor %}
</div>
{% endif %}
<!– End New Category Display –>
</div>
{% endblock %}

You will see the categories are now sorted in order.

categories

 

 

 

Hope you had fun customizing your Portals 🙂

 

-Apurv