Milan Lund logo

Hi, I am Milan Lund!Freelance Web Developer with Kentico Expertise

I specialize in building and maintaining websites on the Kentico platform.

  • Kentico CMS

Custom macro: Get the current user's country and state

In this post I will provide you with a code example of a custom macro that gives you country and state of the current user.

The problem

My client asked me to display country and state of authenticated users on his Kentico website. The information is stored in the UserCountry column of the CMS_User table. Regrettably, the names are stored in the codename format so it is not possible to show them directly with use of the {%CurrentUser.UserCountry%} macro. You would end up with results like AntiguaAndBarbuda or USA;Louisiana.

Initially, I came up with a macro that returns display name of the current user's country: {% GlobalObjects.Countries.Where("CountryName = '" + CurrentUser.UserCountry + "'"+).FirstItem.CountryDisplayName %}. Unfortunatelly, the macro ignores states. It displays country names as you would expect i.e. "Antigua and Barbuda" but for USA it returns nothing.

Custom macro is the solution

It would be painful to come up with a macro that takes states into account in Portal Engine. I decided to create a custom macro with use of the Custom macro fields. See the documentation for extending the Kentico macro engine by adding custom macro fields. Below you can see full code for the macro with comments. See the getUserCountryName method if you are interested only in the logic of getting countries and states. With the code below you can use the {% UserCountryName %} macro in the Portal Engine.


About the author

Milan Lund is a freelance web developer with expertise in Kentico. He specializes in building and maintaining websites on the Kentico platform. Milan writes articles based on his project experiences to assist both his future self and other developers.

Find out more
Milan Lund