Using the SmartObject Runtime API

There are some high-level steps required to use the SmartObject Runtime API effectively:

Copy

1. Build a connection string with the SCConnectionStringBuilder class

// build up a connection string with the SCConnectionStringBuilder
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder hostServerConnectionString = new SCConnectionStringBuilder();
hostServerConnectionString.Host = "localhost"; // name of the K2 host server, or the name of the DNS entry pointing to the K2 Farm
hostServerConnectionString.Port = 5555; // use port 5555 for all non-workflow client connections
hostServerConnectionString.IsPrimaryLogin = true; // true = re-authenticate user, false = use cached security credentials
hostServerConnectionString.Integrated = true; // true = use the logged on user, false = use the specified user
Copy

2. Instantiate and Open the connection to the K2 server

//Open the connection to K2
SourceCode.SmartObjects.Client.SmartObjectClientServer soServer = new SmartObjectClientServer();
soServer.CreateConnection();
soServer.Connection.Open(hostServerConnectionString.ToString());

You can either instantiate the SmartObject with the GetSmartObject method and set up the Method, Input properties and parameters, or use a SQL-like query

Copy

3. Execute the SmartObject and do something with the results

// get an object representing the SmartObject definition so we can set properties or interrogate the SmartObject definition
SmartObject mySmartObject = soServer.GetSmartObject("SmOSystemName");

// set up the Method, Input Properties and Filters, if required
mySmartObject.MethodToExecute = "SmOMethodSystemName";
// declare a value for setting an input property on the method
string value = "[PropertyValue]";
// set the value for an input property of the method
mySmartObject.Methods["SmOMethodSystemName "].InputProperties["smoPropertySystemName"].Value = value;

// execute the method and iterate over the resulting records
SmartObjectList mySmartObjectList = soServer.ExecuteList(mySmartObject);
foreach(SmartObject resultingSmartObject in mySmartObjectList.SmartObjectsList)

{
 // read a property from the SmartObject
 string propertyValue = resultingSmartObject.Properties["smoPropertySystemName"].Value.ToString();
}

You should close the SmartObject server connection when you are done executing methods.

Copy

4. Close the K2 connection

// close the K2 connection
soServer.Connection.Close();

Using a SQL-like statement:

Copy

SQL statement

string sqlquery = "SELECT * FROM SmOName.SmOListMethod";
DataTable dt = soServer.ExecuteSQLQueryDataTable(sqlquery);