Skip to Content

Get company object


I'm creating an addon and it's my first time using the DI API. Everything's working fine so far but whenever I try to debug the addon it takes up to 10 seconds to load the screen. That's because the connection is taking too long (oCompany var). I was wondering if there's a better way to get the oCompany var, maybe using SBO's current connection or something like that. My code is as follow:

private int SetConnectionContext()
            int setConnectionContextReturn = 0;

            string sCookie = null;
            string sConnectionContext = null;

            // First initialize the Company object

            oCompany = new SAPbobsCOM.Company();

            // Acquire the connection context cookie from the DI API.
            sCookie = oCompany.GetContextCookie();

            // Retrieve the connection context string from the UI API using the
            // acquired cookie.
            sConnectionContext = Application.SBO_Application.Company.GetConnectionContext(sCookie);

            // before setting the SBO Login Context make sure the company is not
            // connected

            if (oCompany.Connected == true)

            // Set the connection context information to the DI API.
            setConnectionContextReturn = oCompany.SetSboLoginContext(sConnectionContext);

            return setConnectionContextReturn;

        private int ConnectToCompany()
            int connectToCompanyReturn = 0;

            // Establish the connection to the company database.
            connectToCompanyReturn = oCompany.Connect();

            return connectToCompanyReturn;
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Jan 27, 2017 at 10:10 PM

    Hi Rudá,

    I usually do this:

    SAPbouiCOM.SboGuiApi oSboGuiApi = new SAPbouiCOM.SboGuiApi();
    	// Connect in a SAP Application                                                 
    	SBO_Application = oSboGuiApi.GetApplication(-1);
    	SBO_Application.StatusBar.SetText("Add-on connected: " + appName, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success);
    	SBO_Company = SBO_Application.Company.GetDICompany();
    catch (Exception ex)
    	Log.WriteExceptionLog(ex.Message, ex, this);
    	System.Windows.Forms.MessageBox.Show("It was not possible to connect to SAP Business One: " + ex.Message, "Connection Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);                                                             

    Hope it helps.

    Kind Regards,

    Diego Lother

    Add comment
    10|10000 characters needed characters exceeded