Episerver Personalisation Criteria based on UK postcodes

Recently I come across the requirement to do personalization based on UK postcodes. The format of UK postal codes is very well structured. It consists of two alphanumeric codes called “Outward code” and “Inward code”.

Outward code consists 2 to four letter defining area and district whereas Inward code consists of three letters defining sector and unit.

Episerver personalization uk postcode

Now for complete flexibility in this personalization criteria i have taken two input field with option to add wildcard char “*”

The parameters are

  1. OutwardCode
  2. InwardCode

The combination of these parameters gives us options to do personalization as describe in following table

Outward CodeInward Code Behavior
M14WBPersonalized content for customer on this post code only (Same street)
SW1W0*Personalized content for customers on same area, district but different units (So probably customer from nearby streets)
M14*Personalized content for customers on same area, district but different units (So probably customers from nearby streets)
M1*Personalized content for customers whose postcode starts with M1 (Manchester city centre area)Personalized content for customers in Manchester area
M**Personalized content for customers in Manchester area

Getting Customer location.

There are multiple ways to get your customer (or visitor) postcode. The best way is, if customer is registered customer and you have address in profile.

Another option is to get visitor’s geocoordinates (visitor’s latitude and longitude) and after that do reverse geocoding to get address and postcode from latitude and longitude. Once we have postcode, we can use it to apply this personalization criteria.

In this example I have hard coded customer postcode.

Create Personalization criteria

UkPostcodeCriterionSettings Class:

public class UkPostcodeCriterionSettings : CriterionModelBase
        public string OutwardCode { get; set; }
        public string InwardCode { get; set; }
        public override ICriterionModel Copy()
            return ShallowCopy();

UkPostcodeCriterion Class:

        Category = "Geolocation",
        DisplayName = "UK Postal Codes",
        Description = "Personalization criteria based on customer's UK address")]

    public class UkPostcodeCriterion : CriterionBase<UkPostcodeCriterionSettings>
        public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
            //user location
            var visitorlocation = GetVisitorPostcode();

            // Wildcard in OutwardCode
            if (Model.OutwardCode.Contains("*"))
                var nOutwardcode = Model.OutwardCode.Replace("*", "");

                return visitorlocation.OutwardCode.StartsWith(nOutwardcode);

            // Wildcard in InwardCode
            if (Model.InwardCode.Contains("*"))
                var nInwardcode = Model.OutwardCode.Replace("*", "");

                return visitorlocation.OutwardCode == Model.OutwardCode && visitorlocation.OutwardCode.StartsWith(nInwardcode);

            // no Wildcard
            if (!Model.OutwardCode.Contains("*") && !Model.InwardCode.Contains("*"))
               return visitorlocation.OutwardCode == Model.OutwardCode && visitorlocation.InwardCode == Model.InwardCode;

            return false;

        private Postcode GetVisitorPostcode()

            //ToDo: Get Customer postcode
            var postcode = "M2 2AN";

            var userLocation = new Postcode();
            var codes = postcode.Split(new[] {' '});

            if (codes.Any() && codes.Length == 2)
                userLocation.OutwardCode = codes[0].ToUpper();
                userLocation.InwardCode = codes[1].ToUpper();
            return userLocation;

Source Code: The source code of this Personalisation criteria can be downloaded from this link.

About the author

Naveed Ul-Haq

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *