Go to main content

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.

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

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.

using CMS;
using CMS.DataEngine;
using CMS.MacroEngine;
using CMS.Membership;
using CMS.Globalization;


// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomMacroModule))]

public class CustomMacroModule: Module {
 // Module class constructor, the system registers the module under the name "CustomMacros"
 public CustomMacroModule(): base("CustomMacros") {}

 // Contains initialization code that is executed when the application starts
 protected override void OnInit() {
  base.OnInit();

  // Adds the 'UserCountryName' field to the global macro resolver
  MacroContext.GlobalResolver.SetNamedSourceDataCallback("UserCountryName", getUserCountryName);
 }

 // Callback method that defines the result of the 'UserCountryName' macro field
 private object getUserCountryName(EvaluationContext context) {
  string countryName = "";

  // Gets the current user
  UserInfo user = UserInfoProvider.GetFullUserInfo(MembershipContext.AuthenticatedUser.UserID);

  // Checks that the user exists
  if (user != null) {
   // Get user country
   countryName = user.GetValue("UserCountry", "");

   // Splits the country and gets country and state code names 
   string[] countryState = countryName.Split(';');

   // Gets country
   CountryInfo country = CountryInfoProvider.GetCountryInfo(countryState[0]);

   if (country != null) {
    // Gets country name  
    countryName = country.CountryDisplayName;

    // Is there a state as well
    if (countryState.Length > 1) {

     // Get state
     StateInfo state = StateInfoProvider.GetStateInfo(countryState[1]);

     // Compose full coutry and state output
     if (state != null) {
      countryName += ", " + state.StateDisplayName;
     }
    }
   }
  }

  return countryName;
 }

}

Further reading

all posts
  • Boost Conversions: Persist Form Data After Redirect in Xperience by Kentico

    When users submit a form and are redirected to a thank-you page, a fundamental challenge emerges: the form submission data vanishes. This happens because HTTP redirects are statele…

  • Kentico Xperience

    Do not run Kentico in a shared hosting environment

    Usually, when you want to run your small website you expect low requirements for a web hosting environment and you probably think of an economic shared web hosting service. Unfortu…

  • Kentico Xperience

    Easter eggs in Kentico

    What’s really great about having friends who work at Kentico is that I not only have people to play sports with or hang out with, but I also get insider updates about what’s happen…