Get Current Location from the Device in Dynamics 365

By | March 3, 2017

Introduction:

Dynamics 365 is a leap forward from previous versions in terms of features and improvements. Even the mobile and tablet app of Dynamics 365 is updated with useful features to get the most out of the Dynamics CRM platform.

Dynamics 365 Mobile and Tablet app now offers the ability to get the current user location from the device. This is really helpful to track the location of CRM users.

How the ability to get users location can be helpful for Managers?

It is important for Managers to keep a track of what his team is up to. Tracking users’ performance is one way to ensure that users are working on their assigned projects and keep a track of their progress. Sometimes it is important for a Manager to know the exact location of the user.

Let us see an example where we want to get the current user location in Dynamics 365 Mobile and Tablet app:

1. First we create a custom entity ‘User Location’, which will show the current user location (latitude & longitude), date and time.

2. Before getting the location of the current user from the device, we need to configure the settings on the device.

3. On the Dynamics 365 mobile and tablet app, navigate to Home->Settings as shown in the screenshot below;

Get Current Location from the Device in Dynamics 365

4. Now navigate to Settings->Device Integration Settings. Configure the setting to set ‘User Content and Location’ to ‘On’ and Click ‘OK’, as seen in the screenshot below;

Get Current Location from the Device in Dynamics 365

5. We will now create a button ‘Check In’ on the user ‘Home’ page as shown in the screenshot below;

Get Current Location from the Device in Dynamics 365

6. To get the current location on the click of the button, we have written the below mentioned script;

function getcurrentLocation() {
//get the location latitude and longitude using the getCurrentPosition

    Xrm.Utility.getCurrentPosition().then(function (location) {
      
        var latitude=location.coords.latitude;
        var longitude= location.coords.longitude;

        //get logged in user
        var userId = Xrm.Page.context.getUserId();
        //get logged in user name
        var userName = Xrm.Page.context.getUserName();
        //create organization
        var organizationUrl = Xrm.Page.context.getClientUrl();
        //create user location object
        var userlocation = {};
        userlocation["new_name"] = userName;
        //set the current location in the latitude and longitude field
        userlocation["new_latitude"] = latitude;
        userlocation["new_ longitude "] = longitude;
        userlocation["ownerid_systemuser@odata.bind"] = "/systemusers(" + userId + ")";
        var req = new XMLHttpRequest();
        req.open("POST", organizationUrl + "/api/data/v8.1/new_userlocations", false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Prefer", "odata.include-annotations=*");
        req.onreadystatechange = function () {

            if (this.readyState == 4) {
                req.onreadystatechange = null;
                if (this.status == 204) {
                    //record created successfully
                    Xrm.Utility.alertDialog("Location is tracked");
                } else {
                    var error = JSON.parse(this.response).error;
                    Xrm.Utility.alertDialog(error.message);
                }
            }
        };
        //send request to web API
        req.send(window.JSON.stringify(userlocation));
    }

    }, function (error) {
        Xrm.Utility.alertDialog(error.message);
    });
}

 7. The script mentioned above will create the “User Location” record, set the Latitude and Longitude.

Note: To get the current location’s latitude and longitude, the user needs to use the code mentioned below;

Xrm.Utility.getCurrentPosition().then(function (location) { dosomething},function (error) { dosomething })

 

Conclusion: 

By following the steps mentioned above, a Manager can easily get the current location of the user in Dynamics 365. This is helpful in situations where a Manager needs to reassign certain tasks based on the current location of the users to make the most out of his resources.

Now find customers within X miles of the route using Maplytics ~ try today!

3 thoughts on “Get Current Location from the Device in Dynamics 365

  1. Clara

    Hello, I have an error replicating the sample code. Specifically it says that this.status = 404 … can you help me?

    1. inogic

      Could you please check the following setting to get current location.
      • Switch on the GPS of the mobile.
      • In App Setting Allow Location for MS Dynamics 365.
      • Enable the User Content and Localization in the mobile setting.

      If you still getting the 404 error then please retry from another location.

      Hope this will help you.

      Thanks!

  2. Clara

    I found the error, it was the name of the personalized entity, it should be in the plural. In a few words a letter caused all the error. It works perfectly now.

    Thank you!!

Comments are closed.