Tuesday, September 1, 2015

OnBase Unity C# Visual Studio: Create an ODBC connection

Core to creating an OnBase Visual Studio application is to connecting to and querying the onbase database instance for various activities outside of what the API exposes. Here are the steps to do this:


  1. Install the ODBC driver for your OnBase database
  2. Configure a Data Source Name entry
  3. Import the ODBC class for reference
  4. Write code to establish a connection object
  5. Open a connection
  6. Run through the results
  7. Close a connection
Here's some of the code to help do this:

... main code


                OdbcConnection conn = openODBCConnection();
                OdbcDataReader dr = null;
                OdbcCommand cmd = null;

                cmd = new OdbcCommand("select a.keyvaluechar from keyitem141 a  " +
                "where a.itemnum = '" + itemnumtemp + "'", conn);

                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    TempValue = dr[0].ToString().Trim();
                }

                conn.Close();
                cmd.Dispose();
                dr.Dispose();

... connection method
                 conn.ConnectionString = "DSN=OnBaseDSN;UID=hsi;PWD=password"

OnBase Unity C# Visual Studio: update a keyword value

Ok, from previous posts, we know how to do the following: create an app connection, get a doc ID, create a doc object. Once we have those, we can modify a keyword's value. Note: this is not the same as adding a keyword, which is what you want to do if one doesn't exist. Hope this helps...

                        // check whether document was locked
                        if (documentLock.Status == DocumentLockStatus.LockObtained)
                        {

                            KeywordModifier keyModifier = doc.CreateKeywordModifier();

                            KeywordType searchKeywordType = g_Application.Core.KeywordTypes.Find("Your Date");

                            // loop thru keyword records
                            foreach (KeywordRecord keyRecord in doc.KeywordRecords.FindAll(searchKeywordType))
                            {
                                // loop though keywords
                                foreach (Keyword keywordOld in keyRecord.Keywords.FindAll(searchKeywordType))
                                {

                                    // Find Keyword Type 
                                    KeywordType keywordType0 = core.KeywordTypes.Find("Your Date");

                                    // Create Keyword Object
                                    DateTime newdate1 = DateTime.ParseExact(strDate,"MM/dd/yyyy", null);
                                    string strDatex = newdate1.ToString("MM-dd-yyyy");
                                    DateTime NewDateValue = Convert.ToDateTime(strDatex);

                                    Keyword keywordNew = keywordType0.CreateKeyword(NewDateValue);
                        
                                    //update keyword
                                    keyModifier.UpdateKeyword(keywordOldkeywordNew);

                                  
                                }
                            }

                            //Apply changes
                            keyModifier.ApplyChanges();

                            //release doc lock
                            documentLock.Release();
                           }

OnBase Unity C# Visual Studio: attach a lifecycle

As I've shown in a previous post, you'll need to create an application connection, find the doc handle of the doc you want to attache a lifecycle to, and create the doc object. Once you'll done that, you'll need to create the workflow and lifecycle objects. Then, add the doc to the lifeycle;


                    Workflow workflow = g_Application.Workflow;
                    LifeCycle lifeCycle = g_Application.Workflow.LifeCycles.Find(<config number of the                       lifecycle);
                    workflow.AddDocumentToLifeCycle(doc, lifeCycle);

OnBase Unity C# Visual Studio code to create a document object

In many cases you'll need a document object to perform actions on it. Here are the basics:

Create an application session
        AuthenticationProperties authProps = null;
              authProps = Hyland.Unity.Application.CreateDomainAuthenticationProperties(TestAppServer, TestDatabase);
                authProps.LicenseType = LicenseType.Default;
                  g_Application = Hyland.Unity.Application.Connect(authProps);

          Run your query to find the document's handle. Creating a SQL connection and running it is in a separate post.

                  long docID = Convert.ToInt64(dr[0].ToString().Trim());

                  Document doc = g_Application.Core.GetDocumentByID(docID);

          Once you have a document object you can update keys, attach lifecycles, etc.