In this article lets understand the basics of preparing and executing the fetchxml liquid template tag.

Lets first start with the basics of Liquid Tags and Template Tags.

What is a Liquid Tag:

  • Liquid Tags make up the programming logic that tells templates what to do.
  • Tags are wrapped in {% %}.

What is fetchxml Template tag:

  • A Template tag is a category of Liquid Tags.
  • fetchxml template tag, Allows user to query data from Dataverse, and render the results in a page.
  • Following is the syntax:
{% fetchxml resultVariable %}
<!— Fetchxml query -->
...
{% endfetchxml %}
  • resultVariable ‘ in above sample, holds FetchXML query results and a few other attributes.

Now that you got the basics of fetchxml template tag, let’s proceed with its configuration and execution using an example.

Prepare and Execute the fetchxml template tag:

  • I have a following Offer table in my Dataverse with 3 records. Let’s fetch these records and display them on the website’s Home page using the fetchxml template tag.
  • First, generate the Offer table’s Fetch xml. To generate I used Advanced Find option. You can also use the Fetch XML Builder tool from XRM Toolbox.
  • Generated Fetch XML looks as below:
  • Now go to the Portals Designer and open the Home page in VSCode using Edit code option.
  • Place the following fetchxml liquid tag:
{% fetchxml resultOffers %}
        <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
          <entity name="raj_offer">
            <attribute name="raj_offerid" />
            <attribute name="raj_name" />
            <attribute name="statecode" />
            <attribute name="raj_providername" />
            <order attribute="raj_name" descending="false" />
            <filter type="and">
              <condition attribute="statecode" operator="eq" value="0" />
            </filter>
          </entity>
        </fetch>
      {% endfetchxml %}      
      <table>
        <th>Name</th> 
        <th>Provider</th> 
        {% for offer in resultOffers.results.entities %} 
        <tr> 
          <td>{{ offer.raj_name }}</td> 
          <td>{{ offer.raj_providername }}</td> 
        </tr> 
        {% endfor %}
     </table>
  • Save, sync, and refresh the website. You should see the Offer records on Home page.

Understanding the fetchxml template tag code:

In the following fetchxml template tag,

  • resultOffers : Variable holds the results of exeucted Fetch XML.
  • resultOffers.results.entities : Offer records collection.
  • <table>: Used to display offer records in a row and column format.
{% fetchxml resultOffers %}
        <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
          <entity name="raj_offer">
            <attribute name="raj_offerid" />
            <attribute name="raj_name" />
            <attribute name="statecode" />
            <attribute name="raj_providername" />
            <order attribute="raj_name" descending="false" />
            <filter type="and">
              <condition attribute="statecode" operator="eq" value="0" />
            </filter>
          </entity>
        </fetch>
      {% endfetchxml %}
      
      <table>
        <th>Name</th> 
        <th>Provider</th> 
        {% for offer in resultOffers.results.entities %} 
        <tr> 
          <td>{{ offer.raj_name }}</td> 
          <td>{{ offer.raj_providername }}</td> 
        </tr> 
        {% endfor %}

Hope you now understand the basics of how to prepare and execute the FetchXML template tag.

🙂

Advertisements
Advertisements

2 responses to “Power Pages | Prepare and Execute FetchXML Liquid Template Tag”

  1. Power Pages | Make FetchXML Liquid Template Tag reusable | Rajeev Pentyala - Microsoft Power Platform Avatar

    […] my previous article (Prepare and Execute FetchXML Liquid Template Tag), I have explained the basics of preparing and executing the fetchxml liquid template […]

  2. [Quick Tip] Power Pages | Liquid | FetchXML | Read ‘Choice’ Field Value | Rajeev Pentyala - Microsoft Power Platform Avatar

    […] refer to one of my previous articles Prepare and Execute FetchXML Liquid Template Tag on how to use the FetchXML in Liquid […]

Leave a comment