A new feature of Microsoft Dynamics CRM 2013 is the out-of-the-box Bing Maps integration. It provides the user with a convenient map of the record’s address (account, contact, or lead). When the user clicks on the map, all the standard Bing Map features can be utilized: nearby businesses, traffic, directions, streetside, etc. While this feature is very useful, there many situations where you may need to map multiple locations/addresses on one map. This functionality is not provided out of the box. However, you can easily achieve functionality by
- Creating a custom map using Bing Maps API
- Utilizing the new Power BI tools
When using one of these tools, it very useful to have the latitude and longitude of the addresses. Dynamics CRM provides fields to store the latitude and longitude values. Unfortunately, Dynamics CRM does not populate these fields automatically. In this blog, I will review how to create a custom workflow activity that retrieves an address’s latitude and longitude values. The workflow activity will utilize the Bing Maps REST Services.
In subsequent articles, I will review the
- Creating a custom map using Bing Maps AJAX V7
- Using Power BI as a companion tool to Dynamics CRM.
This blog will not cover the CodeActivity interface and plugin/assembly registration. For more information on those topics, please visit the Microsoft Developer Network.
The following URL is the endpoint of the REST service. The location data is appended to the service endpoint using address parameters such as addressLine, adminDistrict, locality, countryRegion, and postalCode. At least one address parameter must be set. Along with the address parameters, two other important parameters are output and key. The output parameter defines the format of the response. The response can be XML or JSON. The key parameter is the Bing Maps API Key. A Bing Maps Key obtained from the Bing Maps Account Center. In this example, we are using the trial key provided in the CRM Dynamics 2013 SDK (note: trial keys sometimes return empty responses due to request limits).
The response returns one or more location resources that contain location data associated with the URL parameter values. The location data for each resource includes latitude and longitude coordinates, the type of location, and the geographical area that contains the location.
Now that we have a basic understanding of the service’s input and output parameters, let’s see how to use the service in a custom workflow activity. In order to execute the request and process the response, three primary classes are used:
- System.Net.HttpWebRequest is used to create and call the request URL
- System.Net.HttpWebResponse is used to retrieve the XML response
- System.Xml.XmlDocument is used to parse the XML response
Once the response is retrieved, XPath is used to retrieve the significant XML nodes (latitude, longitude, etc.). Utility functions were created to perform the repetitive logic: SelectString, SelectDouble, and SelectInteger. The XML node values are set as the workflow activity’s output parameters (matchCode, displayName, latitude, and longitude). In the attached unmanaged solution, the “Geocode: Account Address” calls the custom activity and updates the account record using the output parameters. For a full definition of the C# code and the “Geocode: Account Address” process, download the source and unmanaged solution found in the Resources section.
To test the code and process, download and install the unmanaged solution. If you signed up for a Bing Maps API Key, make sure you change the value set in the “Geocode: Account Address” process.
After you activate the “Geocode: Account Address” process, create a new account record with an address – see screenshot below. Since “Geocode: Account Address” is an asynchronous process, refresh the account form to view the populated GEOCODE section.
Also, two new views have been added to view the geocoding data: “Account Addresses” and “Account Addresses – Geocode Failures”
To fix existing accounts, you can also run the “Geocode: Account Address” process manually.
- Download the unmanaged solution including the assembly, workflow process, new account views, and alterations to the account form
- Download the workflow activity’s full C# code
- Download the example’s full XML response
- Download a CSV file of example accounts/addresses
If you need help integrating Bing maps with your Microsoft Dynamics CRM solution or have other integrations you’d like to do with Microsoft Dynamics CRM 2013, RSM offers a full range of services from implementation and optimization to development and support. Contact our professionals for more information on our services at 855.437.7202 or email@example.com.
By: Steve Campbell – New Jersey Microsoft Dynamics CRM partner