How to track user location in Dynamics 365 for phone App

Track latitude and longitude in Dynamics 365 for phone App

In previous post we discussed How to get address from longitude and latitude in c# | Reverse geo-coding in c#

In this post, i’ll discuss how we can track the location of a user using Dynamics 365 for phone mobile application.


For storing the latitude and longitude of a user we have created a entity named “userlocation”

Fields:- latitude, longitude, Address

I have divided the task into 2 parts.

  1. Getting latitude and longitude of user in app and storing into a userlocation entity record

2. On creation of userlocation record, a plugin convert the latitude and longitude to full address and store it into address field. Plugin code

For testing purposes, Let’s assume we want to track location of user whenever he access any account record.

You can call js function according to your requirement. Like tracking location whenever a field agent complete a work-order.

Javascript code

//To Do
//Call this function on event on which you want to check in the location
//This function will only work for Dynamics 365 app for mobile an tablet and might give error on web client
function getcurrentLocation() {
 //get the location latitude and longitude using the getCurrentPosition
 if (Xrm.Page.context.client.getClient() == "Mobile")
 var client = Xrm.Page.context.client.getClient();
 Xrm.Utility.getCurrentPosition().then(function (location) {
 var latitude = location.coords.latitude;
 var longitude = location.coords.longitude;
 setLocation(longitude, latitude);
 Xrm.Utility.alertDialog("Location info:- latitude " + latitude + "longitude " + longitude);
}, function (error) {
//Function to store longitude and latiude as crm record
function setLocation(longitude, latitude) {

 var userName = Xrm.Page.context.getUserName();
 var entity = {};
 entity.new_name = userName;
 entity.new_longitude = longitude.toString();
 entity.new_latitude = latitude.toString();
 var req = new XMLHttpRequest();"POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_userlocationses", true);
 req.setRequestHeader("OData-MaxVersion", "4.0");
 req.setRequestHeader("OData-Version", "4.0");
 req.setRequestHeader("Accept", "application/json");
 req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
 req.onreadystatechange = function () {
 if (this.readyState === 4) {
 req.onreadystatechange = null;
 if (this.status === 204) {
 var uri = this.getResponseHeader("OData-EntityId");
 var regExp = /\(([^)]+)\)/;
 var matches = regExp.exec(uri);
 var newEntityId = matches[1];
 Xrm.Utility.alertDialog("Location is tracked");
 } else {


I have called getcurrentLocation js function on load of account form.

Let’s see the output in out Dynamics 365 for phone application:

Step 1:  Open the Dynamics 365 for phone app


Step 2: First of all we need to enable location tracking in our application, then only our code will be able to track location using gps of device. Click on settings as shown below:


Step 3: Click on mobile settings:


Step 4: Click on User content and Location and turn it on.


Once clicked OK, you have given the permission to the app to access gps of device.

Step 5: Now, let’s open an account record, on opening the pop up message will show the latitude and longitude of user:-


Step 6: Now, let’s open the userocation entity record just created in out crm:


So here, you can see a record got created in crm at the time of tracking and also our plugin converted the latitude and longitude into address.

Please find plugin solution which convert latitude and longitude into address here

Also,i have uploaded ready to import Dynamics 365 v9.x solution  here .Download, import into your crm org, configure and start using.

Follow for more cool crm codes.

Happy Tracking..!!!

3 thoughts on “How to track user location in Dynamics 365 for phone App

Add yours

      1. That’s interesting, I wasn’t able to get it to work with v9. I was able to get what I needed using the Xrm.Utility.alertDialog();


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: