EPiServer CMS 6 R2 comes with the great new personalisation feature of Visitor Groups. One of the criterion's available to personalise on is the “geographic location” criteria. Put simply this allows editors to specify a location and/or country to personalise content.

Obviously this functionality utilises a Geo/IP lookup database under the hood. The provider is specified in the EPiServerFramework.config file as follows (in R2 Beta):

<geolocation defaultProvider="maxmind">
  <providers>
    <add databaseFileName="C:\Program Files\EPiServer\Framework\6.2.165.1\Geolocation\GeoLiteCity.dat"
      name="maxmind" type="EPiServer.Personalization.Providers.MaxMind.GeolocationProvider, EPiServer.ApplicationModules" />
  </providers>
</geolocation>

I wanted to find a way to use this feature in my own code for use in reporting or possibly in something like a gadget. Allan Thræn had mentioned that it would be available through the EPiServer API so I decided to investigate. It turns out to be remarkably simple:

GeolocationProvider prov = EPiServer.Personalization.Geolocation.Provider as GeolocationProvider;
IPAddress ip = IPAddress.Parse("80.158.171.254");
IGeolocationResult result = prov.Lookup(ip);

This code is also available on the EPiServer code section.

Enjoy!


Comments