![]() |
VOOZH | about |
The CData Cmdlets for Dynamics 365 Business Central are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Dynamics 365 Business Central.
The Cmdlets are not only a PowerShell interface to Dynamics 365 Business Central, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Dynamics 365 Business Central data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Dynamics 365 Business Central. To access Dynamics 365 Business Central data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Dynamics 365 Business Central.
Once you have acquired the necessary connection properties, accessing Dynamics 365 Business Central data in PowerShell can be enabled in three steps.
To authenticate to Dynamics 365 Business Central, you must select an AuthScheme and provide the required properties (OAuth by default).
Specify the . If you have multiple companies in your organization, you must also specify the to indicate which company you would like to connect to. does not need to be specified if you have only one company.
To authenticate with an Access Key, set AuthScheme to "AccessKey" and provide the and properties.
To obtain the and values, navigate to the Users page in Dynamics 365 Business Central and then click on Edit. The User Name and Web Service Access Key values are what you will enter as the and connection string properties. Note that the User Name is not your email address. It is a shortened user name.
If you wish to authenticate through other methods, refer to the Help documentation.
Install the module:
Install-Module D365BusinessCentralCmdlets
Connect:
$d365businesscentral = Connect-D365BusinessCentral -OrganizationUrl "$OrganizationUrl" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$name = "MyAccount" $accounts = Select-D365BusinessCentral -Connection $d365businesscentral -Table "Accounts" -Where "Name = `'$Name`'" $accounts
You can also use the Invoke-D365BusinessCentral cmdlet to execute SQL commands:
$accounts = Invoke-D365BusinessCentral -Connection $d365businesscentral -Query 'SELECT * FROM Accounts WHERE Name = @Name' -Params @{'@Name'='MyAccount'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Dynamics 365 Business Central\lib\System.Data.CData.D365BusinessCentral.dll")
Connect to Dynamics 365 Business Central:
$conn= New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralConnection("OrganizationUrl=https://myaccount.financials.dynamics.com/;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the D365BusinessCentralDataAdapter, execute an SQL query, and output the results:
$sql="SELECT accountid, Name from Accounts"
$da= New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.accountid $_.name
}
Update-D365BusinessCentral -Connection $D365BusinessCentral -Columns @('accountid','Name') -Values @('Myaccountid', 'MyName') -Table Accounts -Id "MyId"
$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("UPDATE Accounts SET Name='MyAccount' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-D365BusinessCentral -Connection $D365BusinessCentral -Table Accounts -Columns @("accountid", "Name") -Values @("Myaccountid", "MyName")
$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("INSERT INTO Accounts (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myName","MyAccount")))
$cmd.ExecuteNonQuery()
Remove-D365BusinessCentral -Connection $D365BusinessCentral -Table "Accounts" -Id "MyId"
$cmd = New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralCommand("DELETE FROM Accounts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.D365BusinessCentral.D365BusinessCentralParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Dynamics 365 Business Central Data Provider to get started:
Download NowLearn more:
👁 Dynamics 365 Business Central (NAV) IconRapidly create and deploy powerful .NET applications that integrate with Dynamics 365 Business Central data including Items, Sales Orders, Purchase Orders, and more!