Custom Search with FetchXML

Search is one of those features that sounds simple until you actually try to build it. On a Power Pages site, the built-in search works fine for a lot of cases. But sooner or later you hit a wall. Maybe you want to search only one table, or filter by a status, or sort the results in a special way, or show the results in a layout that the standard search just does not give you. When that happens, it is time to build your own search, and the best tool for the job is FetchXML.

In this guide I will walk you through how custom search with FetchXML works on Power Pages. I will keep things plain and practical, so even if you have never written a FetchXML query before, you will be able to follow along and build something useful.

What FetchXML Actually Is

FetchXML is the query language that Dataverse uses behind the scenes. Think of it as a way to ask Dataverse a question in a format it understands. You tell it which table you want, which columns to pull back, what filters to apply, and how to sort the results. In return, you get exactly the records you asked for.

The nice part is that Power Pages lets you write FetchXML right inside Liquid, the template language that powers your pages. So you do not need a separate app or a heavy bit of custom code. You write a query, drop it into your page, and loop over the results to show them however you like.

Why Use FetchXML Instead of the Default Search

The standard list search on Power Pages does its job by running FetchXML in the background already. It searches the columns you set up in a list and shows the matches. That is great for quick setups. But you give up control.

With your own FetchXML search, you decide everything. You pick the exact table. You choose which columns get searched. You can add filters that the visitor never sees, like only showing records that are active or only records that belong to the signed-in user. You can sort by date, by name, or by anything else. And you control the look of every result, from a plain list to a rich set of cards. When you need that kind of control, custom FetchXML is the way to go.

The fetchxml Liquid Tag

The heart of this whole approach is the fetchxml Liquid tag. You wrap your query inside it, and Power Pages runs it for you. Here is a basic shape of what it looks like:

{% fetchxml results %}
<fetch>
  <entity name="cr123_product">
    <attribute name="cr123_name" />
    <attribute name="cr123_description" />
    <order attribute="cr123_name" />
  </entity>
</fetch>
{% endfetchxml %}

After this runs, you have a variable called results that holds the records. You can then loop through them and print whatever you want on the page.

Adding the Search Box

A search is not very useful unless the visitor can type something. So the next step is to grab what the user typed and feed it into the query. On Power Pages, you can read the value from the URL using the request object. For example, if your search box sends a value called q, you can read it with request.params['q'].

Here is how a search form might look:

<form method="get">
  <input type="text" name="q" placeholder="Search products..." />
  <button type="submit">Search</button>
</form>

When the visitor types a word and hits the button, the page reloads with that word in the URL. Now your FetchXML query can use it.

Filtering by What the User Typed

This is where the search comes alive. You take the value from request.params['q'] and put it into a filter condition. FetchXML has a like operator that matches partial text, so a search for “phone” will find “smartphone” and “headphones” too.

{% assign term = request.params['q'] %}

{% fetchxml results %}
<fetch>
  <entity name="cr123_product">
    <attribute name="cr123_name" />
    <attribute name="cr123_description" />
    <filter type="and">
      <condition attribute="cr123_name" operator="like" value="%{{ term }}%" />
    </filter>
    <order attribute="cr123_name" />
  </entity>
</fetch>
{% endfetchxml %}

The percent signs around the term tell Dataverse to match the word anywhere inside the column. You can add more conditions inside the filter block, like a check that the record is active, so visitors only ever see the right data.

Showing the Results

Once the query has run, you loop through the records and display them. Liquid makes this easy with a simple for loop.

{% if results.entities.size > 0 %}
  <ul>
  {% for item in results.entities %}
    <li>
      <strong>{{ item.cr123_name }}</strong>
      <p>{{ item.cr123_description }}</p>
    </li>
  {% endfor %}
  </ul>
{% else %}
  <p>No results found. Try another word.</p>
{% endif %}

Notice the check at the top. If there are no matches, you show a friendly message instead of a blank space. Little touches like that make the search feel polished and kind to the user.

Keeping It Safe

A custom search is powerful, so you want to handle it with care. The most important point is table permissions. FetchXML on Power Pages respects the permissions you set up, which means a visitor only sees records they are allowed to see. Always set those permissions on the table you are searching. Do not assume the query alone keeps data private.

You should also be careful with the text a visitor types. Since that value goes straight into your query, you want to clean it a bit. Trimming extra spaces and keeping the value simple helps avoid odd behavior. Power Pages handles a lot of this for you, but it is a good habit to stay aware of what a user can send.

A Few Tips Before You Ship It

Start small. Get one table and one filter working before you add more. Once the basics run, you can layer in extra conditions, sorting, and a nicer layout.

Think about how many records you might pull back. If your table is large, add a count limit to the fetch so the page stays fast. You can also build paging so visitors move through results a page at a time instead of loading everything at once.

Test with real words and with words that match nothing, so you can see both the result list and the empty message. Try it while signed in and signed out if your permissions depend on login.

When you put all of this together, you end up with a search that fits your site exactly. The visitor types a word, your FetchXML query does the work, and the right records show up in a layout you control from top to bottom. That is the real beauty of custom search with FetchXML. It hands you the wheel and lets you build the exact experience your users need.

Share the Post: