D365 CRM Portal: Retrieve Data Using Liquid Templates and FetchXML


Liquid is an open-source template language developed by Shopify and written in Ruby. It is the resolution of Shopify themes and is used to load dynamic content on storefronts.

The codebase was ported to .NET as another open-source project called DotLiquid.  Dynamics 365 Portals (formerly Adxstudio) use DotLiquid as the Liquid Template Engine of their portal. The syntax between the original liquid and dot liquid is very similar. 

Within the CRM portal, we will be using it from the web template to retrieve data from CRM via FetchXML.


Where it can be used?

You can use liquid to create Web Templates, inside Copy (HTML) field of a Web Page, or in Code Snippets.



FetchXML is XML based query language. FetchXml is the query language made for SSRS reports on MS Dynamics OnLine/365. FetchXml supports aggregates (sum, count, etc.) and the grouping of results. FetchXml can be used for complex queries when querying in JavaScript.


It stands for JavaScript Object Notation and it is a way to store and transmit data objects in an efficient and readable manner.  JSON is the format in which our FetchXML results will return. 




Let us consider a scenario in which we will search for an account by an account name and will return only contacts accounts associated with an account we are retrieving.

So follow the following steps:

Create the FetchXML query

Go to Advanced Find in Dynamics 365 create criteria as shown in the following screenshot. Behind the screen it will generate the FetchXML. So to download it click on the Download FetchXML button on top ribbon.

Here is the generated FetchXML

Create Web Template

Now follow the following steps to create a web template:

1. Under the Portals tab in Dynamics 365, click on Web Templates.


2. Click create new.


3. Enter your Liquid code into the Web Template with your revised XML and save.

Name: This is the name of the Web Template

Website: This pull-down has the list of websites that are active in the portal. Select the website that will be using this FetchXML query.


Source: Enter the source code here


Here is the detail explaination of the above code.
Line 1-2: These are the parameters that are being passed into the Web Template

Note:  cacheString is a random number that is used to generate and pass to FetchXML query. 

Line 4: This is a Liquid statement that names the FetchXML query. This line is stats that FetchXML starts here.

Line 5-22: This is a slightly altered version of the FetchXML

Line 23: Close the FetchXML code that we started inline 4.

Note: Get data from the FetchXML and put it in the JSON format. This open square bracket is the start of that. It will allow us to return an array of objects.

After the square bracket, start a "for" loop with liquid to get everything returned from the FetchXML query. Use "for" loop to cycle through the FetchXML query name that we created on line 4 (accounts) and returning each object as an entity named "item".

Line 25-33: The data returned is JSON object. The format is name:value so on line 29 the name is "mainphone" and the value is the telephone1 returned from our FetchXML and contained in the item entity. 

Lines 27-28: The primarycontactid is a lookup in Dynamics 365, the value returned from the FetchXML needs to define whether to get the id or the name. Here we are trying to retrieve both.

Line 34: Here there is a Liquid logic to add a comma to JSON format if there is more than one entity (in this case accounts) returned. 

Note: Here its possible to return more than one entity if needed.

Line 35: Here Liquid for loop is ended and also close square bracket (]).

Create Page Template


Click create new.

Enter the following details and save.


Name: Name of Web Template

Website: Website on which Portal is run

Type: Web Template

Web Template: Select the web template that we created in step 2.

Use Website Header and Footer: Uncheck this if it is selected. It will add the header and footer to the data returned but we only want the JSON data returned.

Description: Add description.

Under the Portals tab in Dynamics 365 select the Web Pages option.


Click create new

Enter in the following data and save.


Name: Name for Web Page

Website: Website for Portal

Parent Page:This sets the parent page and affects the URL address used to call the FetchXML page. Mostly used page is Home page.

Partial URL: Set partial url.

Page Template:Page template from step 3

Publishing State: Select "Published" from this pull down to activate this page on the portal.

Hidden From Sitemap: Set it to Yes to hide Fetch query pages from the sitemap 

Test Query

To test your query we need to log into the portal. Login and then go to the URL you created and enter the parameters. The results can be seen below. It worked, the JSON is returned.

Note: If you only see two empty square brackets " [ ] " nothing got returned from your query. So check for following 3 points.

1. Check your Query

2. The entity you are trying to return is enabled on the portal through security

3. the person logged in has access to that account

Posted: Aug 24, 2018,
Categories: Microsoft Portals,
Comments: 0,
Author: Urish Arora
Rate this article:

Urish AroraUrish Arora

Other posts by Urish Arora

, Contact author

Please login or register to post comments.




Subscribe to our blog

«April 2021»

  • Featured Posts
  • Recent Posts
  • Recent Comments
Dynamics365Authority is a community platform for Professionals and Students to contribute or share their knowledge and skills on Microsoft Dynamics 365 Technologies. Our primary goal is to invite professionals on Dynamics 365 technology across the world to contribute & share their knowledge and skills through their blogs so that we can help other developers.
Stay updated with Dynamics 365 technology. Improve skills and knowledge from our blogs, articles and code snippets. Learn it. Follow professionals and learn from them.
Unified Service Desk Book(Online Edition) 


Stay connected with us on our social media channels for latest articles, blogs posts etc.  We will keep updating regularly on our social media platform and web platform.


Follow us on Facebook



View all our tweets



Join us now



View our blogs



Latest Blog

Posted: Dec 24, 2020

Form Event Programming Using JavaScript in Dynamics 365 - An Overview

In this video you will see the details on 'Form Event Programming using JavaScript in Dynamics 365'. This will also explore the different areas where you can use JavaScript in Dynamics 365. 

Read more
Posted: Dec 24, 2020

Getting Started with PowerApps

This video is a complete package of about the Power Apps. This video starts with the discussion about What is Power Apps and key reasons to use them. The difference between Canvas Apps and Model Driven Apps. In third part you will see a demo on how to create a Canvas App. Similarly in the forth part you will see the demo on how to create a Model Driven App. There is also a topic What is a Common Data Service (CDS) which will be covered in the fifth part and in the last part you will have some useful information about the PowerApps Portal (aka CDS Starter Portal).

Read more
Posted: Dec 23, 2020

Business Rules vs JavaScript Client API Dynamics 365

This video is about the difference between the Business Rules and JavaScript Client API in Dynamics 365 and some limitation of Business Rules over JavaScript Client API.

Read more


Dynamics 365 - Search the library for "Dynamics 365"

  What's New in Dynamics 365

  Dynamics 365 Documentation


  Sign-up for 30 days trial



Recent comments


Nice blog. Very helpful to get all references to white papers at one place.

Previous Next

About Us

Here comes the Dynamics 365 Authority - Great technical blog posts are hidden gems. They are hard to find simply because not enough of us write them in the first place. Yet technical blogging is one of the best things we can do not only for ourselves, but also as members of the wider community of developers.

Dynamics 365 Authority leverages this platform to help developers and users to move from traditional paper-based process to modern digital business process. helps your business grow, evolve and transform. is your Digital Transformation Partner

Recent Posts

Form Event Programming Using JavaScript in Dynamics 365 - An Overview

In this video you will see the details on 'Form Event Programming using JavaScript in Dynamics 365'. This will also explore the different areas where you can use JavaScript in Dynamics...
  • 861
  • Article rating: 5.0

Getting Started with PowerApps

This video is a complete package of about the Power Apps. This video starts with the discussion about What is Power Apps and key reasons to use them. The difference between Canvas Apps and Model...
  • 604
  • Article rating: 5.0

Get in touch

Follow Us


Tag cloud:;; Sales; Leading community site on MS Dynamics 365; Dynamics 365 Authority; Service;Top CRM Blogs;  Digital Transformation; Field Service;Top Articles on MS Dynamics CRM Relationship Insights; Office 365 Security Compliance; Project Service; Microsoft Dynamics 365 for Sales; Relevance Search; Goal Metrics; Activities and Case; Sales Literature;Knowledge Management;

Terms Of UsePrivacy StatementCopyright 2021 by Dynamics365Authority
Back To Top