Integrating Microsoft Dynamics CRM with Bing Maps REST Services

By - June 10, 2014

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

OR

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

  1. Creating a custom map using Bing Maps AJAX V7
  2. 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).

http://dev.virtualearth.net/REST/v1/Locations?adminDistrict=IL&locality=Chicago&postalCode=60606&addressLine=1%20S.%20Wacker%20Drive&output=xml&key=Au9tmAwIDZ2XAQVedm85L51EujHtSfgvdFBLpLXBvH-p94kNrokavf00POMu74Xz

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.

Campbell1_Bing REST Svcs

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:

  1. System.Net.HttpWebRequest is used to create and call the request URL
  2. System.Net.HttpWebResponse is used to retrieve the XML response
  3. System.Xml.XmlDocument is used to parse the XML response

Campbell2_Bing REST Svcs

 

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.

Campbell3_Bing REST Svcs

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.

Campbell4_Bing REST Svcs

 

Also, two new views have been added to view the geocoding data: “Account Addresses” and “Account Addresses – Geocode Failures”

Campbell5_Bing REST Svcs

To fix existing accounts, you can also run the “Geocode: Account Address” process manually.

Resources:

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 crm@mcgladrey.com.

Read more articles on the new release of Microsoft Dynamics CRM 2013.

By: Steve Campbell – New Jersey Microsoft Dynamics CRM partner

Receive Posts by Email

Subscribe and receive notifications of new posts by email.