You can create shareable and secured URLs (aka deep links) to forms in Dynamics 365 for Finance and Operations using URL Generator, i.e. the UrlGenerator class. Deep links can be embedded in reports, emails, external apps, etc.
A deep link points to a specific form in D365FO, but it can also contain parameters to filter data showing on the opening form. For example, we can create a deep link that opens the Customers form but showing only a specific customer.
Learn more on how to create deep links in Microsoft documentation >>
Please note that the following line of code doesn’t work in batch, because the SessionContext object instance is null when running in batch:
var currentHost = new System.Uri(UrlUtility::getUrl());
In the UrlUtility::getUrl() method the following method is executed:
SessionContext serverContext = SessionContext::Get_Current();
The workaround is to use EnvironmentFactory::GetApplicationEnvironment() method instead, in order to read its Infrastructure.HostUrl:
IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();
str currentUrl = env.Infrastructure.HostUrl;
The whole method looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using Microsoft.Dynamics.AX.Framework.Utilities.UrlHelper; using Microsoft.Dynamics.ApplicationPlatform.Environment; public static str generateRecordUrl(str _menuItemName, MenuItemType _menuItemType, DataSourceName _dataSourceName, Map _fieldNameValueMap /* Map(Types::String, Types::String) */, DataAreaId _dataAreaId = curExt()) { str errorMsg = strFmt('Generating a link to %1 failed', _menuItemName); try { //str currentUrl = URLUtility::getUrl(); NOTE: this method doesn't work in batch! IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment(); str currentUrl = env.Infrastructure.HostUrl; System.Uri currentHostUrl = new System.Uri(currentUrl); UrlGenerator urlGenerator = new UrlGenerator(); urlGenerator.HostUrl = currentHostUrl.GetLeftPart(System.UriPartial::Authority); urlGenerator.MenuItemName = _menuItemName; urlGenerator.MenuItemType = _menuItemType; urlGenerator.Partition = getCurrentPartition(); urlGenerator.Company = _dataAreaId; if (_dataSourceName != '' && _fieldNameValueMap != null) { MapEnumerator mapEnumerator = _fieldNameValueMap.getEnumerator(); var requestQueryParameterCollection = urlGenerator.RequestQueryParameterCollection; while (mapEnumerator.moveNext()) { requestQueryParameterCollection.UpdateOrAddEntry(_dataSourceName, mapEnumerator.currentKey(), mapEnumerator.currentValue()); } } str generatedUrl = urlGenerator.GenerateFullUrl().AbsoluteUri; return generatedUrl; } catch (Exception::CLRError) { DocGlobalHelper::handleClrError(funcName(), errorMsg); } catch { DocGlobalHelper::handleError(errorMsg, true, funcName()); } return ''; } |