Using the Capture liquid tag to generate dynamic FetchXML queries in Power Pages Web Templates

michelcarloMicrosoft 36520 hours ago7 Views

Both capture and assign tags are used to handle variables in Liquid. But the capture tag comes with an advantage, it can be used to store HTML contents or even FetchXML queries that can be used across different places.

We can also use the capture tag with logic inside of it to generate dynamic fetch queries, for example if we have some parameters sent on the querystring we generate one query, otherwise we generate another one, and also add whatever parameter sent to the query.

Assign query string parameters to variables

To simplify the logic, before we handle dynamic queries, let’s assign the query string parameters into variables, and set the default values for each:

{% assign currentMonthStart = now | date: "yyyy-MM-01" %}
{% assign fromDate = request.params.fromDate | default: currentMonthStart %}
{% assign toDate = request.params.toDate | default: now | date: "yyyy-MM-dd" %}
{% assign categories = request.params.categories | default: '' | url_decode %}
{% assign currentDate = now | date: "yyyyMMddHHmmss" %}

Dynamically generating the filters

The goal is to generate filters dynamically based on request parameters (fromDate, toDate, and categories). Using the capture tag, we conditionally include these filters.

Submission dates:

{% capture submissionDateFetchXMLFilter %}
  {% if fromDate != "" or toDate != "" %}
  <filter type="and">
    {% if fromDate != "" %}
    <condition attribute="createdon" operator="on-or-after" value="{{fromDate}}" />
    {% endif %}
    {% if toDate != "" %}
    <condition attribute="createdon" operator="on-or-before" value="{{toDate}}" />
    {% endif %}
  </filter>
  {% endif %}
{% endcapture %}

fromDate and toDate define the date range.

Filter conditions are included only if the respective parameters are provided.

Categories:

{% capture categoriesFetchXMLFilter %}
  {% if categories != '' %}
  <filter>
    <condition attribute="pnp_supportticketcategoryid" operator="in">
      {% assign categoriesArray = categories | split: "," %}
      {% for category in categoriesArray %}
      <value>{{category}}</value>
      {% endfor %}
    </condition>
  </filter>
  {% endif %}
{% endcapture %}

In this case, categories are a string built as an array separated by comma. The code above splits them and creates a dynamic filter using the in operator.

Using the dynamic filters on fetchxml queries

With all filters built, in a FetchXML query, you can simply refer to them as you would to any variable:

{% fetchxml fetch_tickets %}
<fetch top="5000" returntotalrecordcount="true">
  <entity name="pnp_supportticket">
    <attribute name="pnp_ticketnumber" />
    <attribute name="pnp_name" />
    <attribute name="createdon" />
    <order attribute="createdon" descending="true" />
    {{submissionDateFetchXMLFilter}}
    <link-entity name="pnp_supportticketcategory" from="pnp_supportticketcategoryid" to="pnp_supportticketcategoryid" alias="categoryfilter" intersect="true">
      {{categoriesFetchXMLFilter}}
    </link-entity>
  </entity>
</fetch>
{% endfetchxml %}

If we debug the query, the filters will be correctly generated and appended:

<fetch mapping="logical" distinct="true" returntotalrecordcount="true">
  <entity name="pnp_supportticket">
    <attribute name="pnp_details" />
    <attribute name="pnp_supportticketid" />
    <attribute name="pnp_name" />
    <attribute name="pnp_portaluser" />
    <attribute name="pnp_supportticketstatus" />
    <attribute name="pnp_ticketnumber" />
    <attribute name="pnp_internalresponse" />
    <attribute name="createdon" />
    <order attribute="createdon" descending="true" />
    <filter type="and">
      <condition attribute="createdon" operator="on-or-after" value="2025-01-01" />
      <condition attribute="createdon" operator="on-or-before" value="2025-01-08" />
    </filter>
    <filter />    
    <link-entity name="pnp_supportticket_pnp_supportticketcate" from="pnp_supportticketid" to="pnp_supportticketid" intersect="true">
      <link-entity name="pnp_supportticketcategory" from="pnp_supportticketcategoryid" to="pnp_supportticketcategoryid" alias="category" intersect="true">
        <attribute name="pnp_name" />
        <attribute name="pnp_supportticketcategoryid" />
      </link-entity>
    </link-entity>
    <link-entity name="pnp_supportticket_pnp_supportticketcate" from="pnp_supportticketid" to="pnp_supportticketid" intersect="true">
      <link-entity name="pnp_supportticketcategory" from="pnp_supportticketcategoryid" to="pnp_supportticketcategoryid" alias="categoryfilter" intersect="true">
        <filter>
          <condition attribute="pnp_supportticketcategoryid" operator="in">
            <value>f49cd433-2b78-ef11-a670-000d3a2a9c00</value>
            <value>f19cd433-2b78-ef11-a670-000d3a2a9c00</value>
            <value>10f9c140-2b78-ef11-a670-000d3a2a9c00</value>
          </condition>
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>

Conclusion

With the flexibility of Liquid, your FetchXML queries don’t need to be static code blocks but they can also be constructed dynamically.

References

Variable tags – Microsoft Learn

The post Using the Capture liquid tag to generate dynamic FetchXML queries in Power Pages Web Templates appeared first on michelcarlo.

Original Post https://michelcarlo.com/2025/03/02/using-the-capture-liquid-tag-to-generate-dynamic-fetchxml-queries-in-power-pages-web-templates/

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...