![]() |
VOOZH | about |
The CData Cmdlets for SAP Business One 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 SAP Business One.
The Cmdlets are not only a PowerShell interface to SAP Business One, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SAP Business One data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SAP Business One. To access SAP Business One data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SAP Business One.
Once you have acquired the necessary connection properties, accessing SAP Business One data in PowerShell can be enabled in three steps.
To authenticate to SAP Business One you must provide the and properties.
To connect to data, specify . This is your SAP Business One Service Layer root URL.
Install the module:
Install-Module SAPBusinessOneCmdlets
Connect:
$sapbusinessone = Connect-SAPBusinessOne -Url "$Url" -User "$User" -Password "$Password" -CompanyDB "$CompanyDB" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$doctype = "dDocument_Items" $orders = Select-SAPBusinessOne -Connection $sapbusinessone -Table "Orders" -Where "DocType = `'$DocType`'" $orders
You can also use the Invoke-SAPBusinessOne cmdlet to execute SQL commands:
$orders = Invoke-SAPBusinessOne -Connection $sapbusinessone -Query 'SELECT * FROM Orders WHERE DocType = @DocType' -Params @{'@DocType'='dDocument_Items'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SAP Business One\lib\System.Data.CData.SAPBusinessOne.dll")
Connect to SAP Business One:
$conn= New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneConnection("Url=http://localhost:50000/b1s/v1;User=username;Password=password;CompanyDB=dbname;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the SAPBusinessOneDataAdapter, execute an SQL query, and output the results:
$sql="SELECT DocEntry, DocType from Orders"
$da= New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.docentry $_.doctype
}
Update-SAPBusinessOne -Connection $SAPBusinessOne -Columns @('DocEntry','DocType') -Values @('MyDocEntry', 'MyDocType') -Table Orders -Id "MyId"
$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("UPDATE Orders SET DocType='dDocument_Items' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-SAPBusinessOne -Connection $SAPBusinessOne -Table Orders -Columns @("DocEntry", "DocType") -Values @("MyDocEntry", "MyDocType")
$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("INSERT INTO Orders (DocType) VALUES (@myDocType)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myDocType","dDocument_Items")))
$cmd.ExecuteNonQuery()
Remove-SAPBusinessOne -Connection $SAPBusinessOne -Table "Orders" -Id "MyId"
$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the SAP Business One Data Provider to get started:
Download NowLearn more:
👁 SAP Business One IconRapidly create and deploy powerful .NET applications that integrate with SAP Business One data including Accounts, Activities, Orders, Customers, and more!