Power Community

Power Community

2 Liquid tips – Generate GUIDs and base urls in Power Pages

In this post I’m looking at Power Pages and how to get the base url of my site and generate Guids in Liquid.

Power Pages development

Ok, first of all, Power Pages development can be challenging for many. It really helps if you are good at the following technologies:

  • HTML
  • CSS
  • JavaScript
  • Dataverse
  • Liquid

Now assuming that you have a basic understand of the above, I’ve got 2 quick tips to hopefully make your Power Pages development a little bit easier.

Tip 1 – Get your Power Pages URL

Consider that on one of your pages you have the following url used within a link:

Place Order

Now when you deploy this to a different environment that base url part will change. However you wouldn’t want to have to edit the pages. Especially if you have many pages with these kind of links.

The better option is two rewrite your code a bit. Using a base url is better:

Place Order

So now we will need to get a base url. This using liquid filters this is actually very easy. A single one liner will do.

{% assign baseurl = request.url |split: request.path | first %}

So qwe take the current page url split this by the current page url so that we get an array with the base url and the query strings. Then by piping this into the first function we take the base url.

Tip 2 – Create a unique ID

Within Liquid you might want to generate a guid sometimes. Unfortunately there isn’t a function Guid() available like in many other languages.

But what if you want to create record using a form and then a few pages down the user journey, you want to update that same records again. Being able to pass a unique ID from one page to another can be quite useful.

button with link to page needing base url and guid.

I’ve seen some solutions by using the current date and time, however two users clicking your buttons at the same time would mess things up.

In this case we have a URL on a page that calls a page where we want to create a booking. Now to generate a unique booking id we would generate a unique if like this:


Out of interest I also tried to use AI to give me the answer, but new_guid doesn’t exist in Liquid:

Generate Guid the wrong way

Last week when I searched for the same the AI expert came up with something like this:

'xxxxxxxxxxxxxxx' | replace: 'x', '01234567890abcdef'

But once again that also didn’t work as each x will be replaced with all 16 characters.

&bookingid={% for character in 'xxxxxxxxxxxxxxx' %}{{'01234567890abcdef'| shuffle | first }}{% endfor %}

So now we have a GUID that isn’t following the standard guid format. Just with a bit more liquid logic we can generate a proper GUID.

&bookingid={% for character in 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' %}
  {%if character =="x" %}
    {{'01234567890abcdef'| shuffle | first }}{% endif %}
  {%if character != "x" %}
  {% endif %}
{% endfor %}"

This post was originally published on this site

- Advertisement -spot_img


Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

6 User-Friendly steps to connect Dynamics 365 CRM with Mailchimp – Effortless Integration!

As a CRM user, you understand the importance of managing customer relationships effectively. But what about integrating your marketing...

More Articles Like This

- Advertisement -spot_img