{"id":4631,"date":"2017-03-03T19:11:32","date_gmt":"2017-03-03T13:41:32","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=4631"},"modified":"2017-03-03T19:11:32","modified_gmt":"2017-03-03T13:41:32","slug":"get-current-location-from-the-device-in-dynamics-365","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/03\/get-current-location-from-the-device-in-dynamics-365\/","title":{"rendered":"Get Current Location from the Device in Dynamics 365"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Introduction:<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\"><strong>How the ability to get users location can be helpful for Managers?<\/strong><\/p>\n<p style=\"text-align: justify;\">It is important for Managers to keep a track of what his team is up to. Tracking users\u2019 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.<\/p>\n<p style=\"text-align: justify;\"><strong>Let us see an example where we want to get the current user location in Dynamics 365 Mobile and Tablet app:<\/strong><\/p>\n<p style=\"text-align: justify; padding-left: 30px;\">1. First we create a custom entity \u2018User Location\u2019, which will show the current user location (latitude &amp; longitude), date and time.<\/p>\n<p style=\"text-align: justify; padding-left: 30px;\">2. Before getting the location of the current user from the device, we need to configure the settings on the device.<\/p>\n<p style=\"text-align: justify; padding-left: 30px;\">3. On the Dynamics 365 mobile and tablet app, navigate to Home-&gt;Settings as shown in the screenshot below;<\/p>\n<p style=\"padding-left: 30px; text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-4630\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/03\/Get-Current-Location-from-the-Device-in-Dynamics-365.jpg\" alt=\"Get Current Location from the Device in Dynamics 365\" width=\"270\" height=\"469\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: justify;\">4. Now navigate to Settings-&gt;Device Integration Settings. Configure the setting to set \u2018User Content and Location\u2019 to \u2018On\u2019 and Click \u2018OK\u2019, as seen in the screenshot below;<\/p>\n<p style=\"text-align: justify; padding-left: 30px;\"><img decoding=\"async\" class=\"aligncenter  wp-image-4639\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/03\/Untitled-designsdfsw-1024x701.png\" alt=\"Get Current Location from the Device in Dynamics 365\" width=\"661\" height=\"452\" \/><\/p>\n<p style=\"padding-left: 30px;\">5. We will now create a button \u2018Check In\u2019 on the user \u2018Home\u2019 page as shown in the screenshot below;<\/p>\n<p style=\"padding-left: 30px;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-4629\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/03\/4Get-Current-Location-from-the-Device-in-Dynamics-365.jpg\" alt=\"Get Current Location from the Device in Dynamics 365\" width=\"302\" height=\"514\" \/><\/p>\n<p style=\"padding-left: 30px;\">6. To get the current location on the click of the button, we have written the below mentioned script;<\/p>\n<pre class=\"lang:default decode:true\">function getcurrentLocation() {\n\/\/get the location latitude and longitude using the getCurrentPosition\n\n    Xrm.Utility.getCurrentPosition().then(function (location) {\n      \n        var latitude=location.coords.latitude;\n        var longitude= location.coords.longitude;\n\n        \/\/get logged in user\n        var userId = Xrm.Page.context.getUserId();\n        \/\/get logged in user name\n        var userName = Xrm.Page.context.getUserName();\n        \/\/create organization\n        var organizationUrl = Xrm.Page.context.getClientUrl();\n        \/\/create user location object\n        var userlocation = {};\n        userlocation[\"new_name\"] = userName;\n        \/\/set the current location in the latitude and longitude field\n        userlocation[\"new_latitude\"] = latitude;\n        userlocation[\"new_ longitude \"] = longitude;\n        userlocation[\"ownerid_systemuser@odata.bind\"] = \"\/systemusers(\" + userId + \")\";\n        var req = new XMLHttpRequest();\n        req.open(\"POST\", organizationUrl + \"\/api\/data\/v8.1\/new_userlocations\", false);\n        req.setRequestHeader(\"Accept\", \"application\/json\");\n        req.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\n        req.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\n        req.setRequestHeader(\"OData-Version\", \"4.0\");\n        req.setRequestHeader(\"Prefer\", \"odata.include-annotations=*\");\n        req.onreadystatechange = function () {\n\n            if (this.readyState == 4) {\n                req.onreadystatechange = null;\n                if (this.status == 204) {\n                    \/\/record created successfully\n                    Xrm.Utility.alertDialog(\"Location is tracked\");\n                } else {\n                    var error = JSON.parse(this.response).error;\n                    Xrm.Utility.alertDialog(error.message);\n                }\n            }\n        };\n        \/\/send request to web API\n        req.send(window.JSON.stringify(userlocation));\n    }\n\n    }, function (error) {\n        Xrm.Utility.alertDialog(error.message);\n    });\n}\n<\/pre>\n<p style=\"padding-left: 30px;\">\u00a07. The script mentioned above will create the \u201cUser Location\u201d record, set the Latitude and Longitude.<\/p>\n<blockquote><p><em><strong>Note:\u00a0<\/strong><\/em><em>To get the current location\u2019s latitude and longitude, the user needs to use the code mentioned below;<\/em><\/p>\n<p><em><strong>Xrm.Utility.getCurrentPosition().then(function (location) { dosomething},function (error) { dosomething })<\/strong><\/em><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Conclusion:\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\"><em><strong><a href=\"http:\/\/www.maplytics.com\/radius-search-dynamics-crm\" target=\"_blank\" rel=\"noopener noreferrer\">Now find customers within X miles of the route using Maplytics ~ try today!<\/a><\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/03\/get-current-location-from-the-device-in-dynamics-365\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":4628,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16],"tags":[440,983,1187],"class_list":["post-4631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","tag-current-user-location-in-dynamics-365-mobile-and-tablet-app","tag-latitude-longitude-in-dynamics-365-mobile-and-tablet-app","tag-mobile-and-tablet-app-of-dynamics-365"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=4631"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/4631\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/4628"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=4631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=4631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=4631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}