Connect-ERP with Microsoft Dynamics NAV does not work

Issue

When trying to connect to Microsoft Dynamics NAV using Connect-ERP the Cmdlet returns $false and the connection is not established.

Cause

The Microsoft Dynamics NAV page service does not provide a $metadata document at the service URL.

Solution

The following additional code is required to successfully connect to Microsoft Dynamics NAV.

The code adds "/$metadata" to the RequestURL. "/$metadata" is required by powerGate to get the metadata of the service.

Import-Module PowerGate

$NavBaseUrl = 'http://machine:7048/DynamicsNAV100/OData'
$ODataUrl = "http://machine:7048/DynamicsNAV100/OData/Company('coolOrange')"

Connect-ERP -Service $ODataUrl -OnConnect {
param($settings)
$settings.BeforeRequest = [Action[System.Net.Http.HttpRequestMessage]] {
param($request)
if($request.RequestUri.ToString().EndsWith('/$metadata')){
$request.RequestUri = $NavBaseUrl+'/$metadata'
}
}
}

See Also