Thursday, December 30, 2010

CRM 2011 Beta :- GetGeoCode activity for the CRM Workflows

With Maps becoming more and more popular way of looking at the data like addresses it has become necessary that we save the addresses of the entities in longitude and latitude format so as to ensure that it can be displayed on the maps easily. 
I have an entity called School in my CRM 2011 Beta system and it has an address associated with it. A few of the attributes of entity are 
1) Name 
2) AddressLine
3) City
4) PostalCode
5) State
6) Country (Defaulted to USA for this discussion) 
7) A few more attributes which are not relevant for this discussion


There's a CRM Form where the user of the system goes and creates the entity instances of the system. The requirement is to show all the schools in the CRM system on a Bing Map. Now this can be achieved using various controls and SDKs like Telerik Controls, Bing Map SDK, Open Map Provider for Telerik Controls and some Google Map APIs as well. But most of these SDKs expect a Longitude and Latitude to be provided for the address so that they can quickly pinpoint the location on their map. 


Longitude and Latitude is something i can't ask the user to fill as many end users might not understand what it means and most won't have any idea about the latitude and longitude on which the school is present on the map. So the easiest way is to achieve it using a plug in or workflow which can run asynchronously to populate the longitude and latitude of the school entity instance. I chose to do it using a workflow process. 
But the question is how would the workflow process determine the longitude and latitude given the Address. The answer to this is that there are various Geocode APIs available. A few popular ones are 
1) Yahoo PlaceFinder API 
2) Google GeoCode API
3) Bing GeoCode Service
4) Bing Locations API


In my case i chose to use Bing Locations API to find the longitude and location of the given address. 


It is a rest based service and can be called by shooting a GET request on the URL in the following format 


http://dev.virtualearth.net/REST/v1/Locations/{CountryCode}/{StateCode}/{PostalCode}/{City}/{StreetAddress}?output=xml&key={BingMapKey} 


You can get a BingMap API access key for free by creating an account on https://www.bingmapsportal.com/ 


The sample CRM Activity code which i wrote can be downloaded from here. You can compile this code into a dll. Sign it with a key and register the same using CRM Plugin Registration Utility which ships with CRM SDK. 


Njoi the APIs. 


~Abhishek



3 comments:

Unknown said...

Very useful post. thanks. I managed to get this to work on onpremise but was unable to register it in Sandbox isolation mode. I'm getting an error "Since some of the plug-ins cannot be isolated, the assembly cannot be marked as Isolated." Is there any way it can be registered in Sandbox isolation mode for CRM 2011 online?

Unknown said...

Very useful post. thanks. I managed to get this to work on onpremise but was unable to register it in Sandbox isolation mode. I'm getting an error "Since some of the plug-ins cannot be isolated, the assembly cannot be marked as Isolated." Is there any way it can be registered in Sandbox isolation mode for CRM 2011 online?

luckyabhishek said...

Naim I believe it should be possible to get this registered in sandbox mode as all the plugin does is make a rest call. Is your assembly referring some other assemblies or using some other code which can't run in sandbox?