Wednesday, 25 June 2025

Retrieve records using liquid template in power pages

 To retrieve a record from an entity using Liquid code in a Power Pages or Dynamics 365 portal, you can use the entities object and its associated tags, often in conjunction with FetchXML or OData queries. The basic approach involves identifying the entity, optionally filtering the records, and then accessing the desired attributes of the retrieved record. 

Here's a breakdown of the key aspects:

1. Identify the Entity and Record:
  • Entity Name:
    Determine the logical name of the entity you're working with (e.g., accountcontactnew_customentity). You can find this in the Power Platform admin center or through the API. 
  • Record Identifier:
    You'll typically need a way to identify the specific record you want to retrieve. This could be a GUID (Globally Unique Identifier), a unique field value, or based on a filter within a FetchXML or OData query. 
2. Using entities and Liquid Tags:
  • Accessing the entities object:
    Liquid provides access to the entities object, which allows you to interact with Dataverse records.
  • Fetching Records:
    You can use FetchXML or OData queries within Liquid to retrieve records. For example:
    • FetchXML: {% fetchxml my_query %} <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> <entity name="account"> <attribute name="name" /> <filter type="and"> <condition attribute="accountid" operator="eq" value="{{ record_id }}" /> </filter> </entity> </fetch> {% endfetchxml %} 
    • OData: {% assign record = entities.account | where: "accountid", record_id | first %} 
  • Accessing Attributes:
    Once you have the record (e.g., record in the above examples), you can access its attributes using dot notation:
    • {{ record.name }} (for the name attribute)
    • {{ record.primarycontactid.name }} (if primarycontactid is a lookup field) 
3. Example using FetchXML: 
Code
{% fetchxml my_query %}<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">  <entity name="account">    <attribute name="name" />    <attribute name="telephone1" />    <filter type="and">      <condition attribute="accountid" operator="eq" value="{{ request.params.accountid }}" />    </filter>  </entity></fetch>{% endfetchxml %}{% for account in my_query.results.entities %}  <h2>{{ account.name }}</h2>  <p>Phone: {{ account.telephone1 }}</p>{% endfor %}
4. Example using OData:
Code
{% assign account_id = request.params.accountid %}{% assign account = entities.account | where: "accountid", account_id | first %}{% if account %}  <h2>{{ account.name }}</h2>  <p>Phone: {{ account.telephone1 }}</p>{% else %}  <p>Account not found.</p>{% endif %}
5. Important Considerations:
  • Permissions:
    Ensure that the web roles assigned to the portal users have the necessary table permissions to view the entity and its attributes. 
  • Performance:
    When dealing with large datasets, optimize your queries (FetchXML or OData) to retrieve only the necessary columns using $select and $expand options. This can significantly improve performance. 
  • Error Handling:
    Implement error handling (e.g., checking if a record exists before accessing its attributes) to provide a better user experience. 
  • Related Records:
    When working with lookup fields, you can retrieve related records using the same entities object and appropriate filtering. You can also use $expand in OData to retrieve related records in the same query. 
  • Web Templates:
    Liquid code for retrieving and displaying data is often used within web templates, which are then associated with web pages or other components. 

No comments:

Post a Comment

Filtered View in Dynamcis crm

  Dynamics CRM provides filtered views so we can access CRM data in SQL. To access filtered views, go to the Dynamics CRM company database a...