What Is SAP DI API

21 June 2017 - 1701 views
The SAP DI API (Data Interface API) is part of the SAP Business One Software Development Kit (SDK) that allows developers to interact with the database. The API exposes objects and methods that can be used to read, write, update and delete data. The API is COM based, so there are .Net and Java interfaces.

The SDK and supporting files are provided with SAP Business One. This article focuses on a simple .Net console application that will list all companies setup in Business One. First ensure that you have located the following DLL file SAPbobsCOM90.dll. You will need to add a refernce to this DDL in your .Net project.

Open Visual Studios and create a new Console Application. From the Project menu click Add Reference. Browse for SAPbobsCOM90.dll.

The API requires authentication, which means you will first need to login to a company.

Listing 1

SAPbobsCOM.Company comp = new SAPbobsCOM.Company();
comp.Server = "server ip:30015";
comp.CompanyDB = "db name";
comp.DbUserName = "db username";
comp.DbPassword = "db password";
comp.UserName = "username";
comp.Password = "password";
comp.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_HANADB;

comp.Connect();

Listing 1 above shows the authentication and connection code. Notice that the DbServerType is set to HANADB. If you are using MSSQL, you will need to change the server type to dst_MSSQL*.

Errors are handled by the GetLastError() method of the Company object. This method accpets two paramaters, which must be passed by reference. The first argument is errCode, which must be of type int and the second is errMsg which must be of type string.

Listing 2

int errCode = 0;
string errMsg = "";

comp.GetLastError(out errCode, out errMsg);

if (errCode < 0)
{
    Console.WriteLine(errMsg);
}

If errCode is below 0, this means an error has occured. You should then check errMsg to determine what the error was. If errCode is 0 then no error occured.

If the connection was successful, you can then call the GetCompanyList() method on the comp object to get a Recordset containing all companies.

Listing 3

SAPbobsCOM.Recordset result = comp.GetCompanyList();

while (!result.EoF)
{
    Console.WriteLine(result.Fields.Item(0).Value);
    result.MoveNext();
}