Ready to get started?

Learn more about the CData ADO.NET Provider for SAP Business One or download a free trial:

Download Now

Automate SAP Business One Integration Tasks from PowerShell

Are you looking for a quick and easy way to access SAP Business One data from PowerShell? We show how to use the Cmdlets for SAP Business One and the CData ADO.NET Provider for SAP Business One to connect to SAP Business One data and synchronize, automate, download, and more.

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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the SAP Business One API, 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.

After obtaining the needed connection properties, accessing SAP Business One data in PowerShell consists of three basic steps.

To authenticate to SAP Business One you must provide the Userand Passwordproperties.

To connect to data, specify Url. This is your SAP Business One Service Layer root URL.

PowerShell

  1. Install the module:

    Install-Module SAPBusinessOneCmdlets
  2. Connect:

    $sapbusinessone = Connect-SAPBusinessOne -Url "$Url" -User "$User" -Password "$Password" -CompanyDB "$CompanyDB"
  3. 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'}

ADO.NET

  1. 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")
  2. 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()
  3. 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 SAP Business One Data

PowerShell

Update-SAPBusinessOne -Connection $SAPBusinessOne -Columns @('DocEntry','DocType') -Values @('MyDocEntry', 'MyDocType') -Table Orders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("UPDATE Orders SET DocType='dDocument_Items' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert SAP Business One Data

PowerShell

Add-SAPBusinessOne -Connection $SAPBusinessOne -Table Orders -Columns @("DocEntry", "DocType") -Values @("MyDocEntry", "MyDocType")

ADO.NET

$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("INSERT INTO Orders (DocType) VALUES (@myDocType)", $conn) $cmd.Parameters.Add(new System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myDocType","dDocument_Items")) $cmd.ExecuteNonQuery()

Delete SAP Business One Data

PowerShell

Remove-SAPBusinessOne -Connection $SAPBusinessOne -Table "Orders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.SAPBusinessOne.SAPBusinessOneCommand("DELETE FROM Orders WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.SAPBusinessOne.SAPBusinessOneParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()