How to return data from a SmartObject

In the following examples, you access a deployed SmartObject and return data from it. The first is a scalar method example that shows how to return data based on a single ID. The second is a list method example that shows how to return all records from the SmartObject.

See Also: How to filter data returned from a SmartObject

The SmartObject has the following properties:

Name: Employee

Properties:

  • ID (Autonumber)
  • FirstName (Text)
  • LastName (Text)
  • Email (Text)

In the following example, provide values for the variables that begin with the underscore (_) character to match your environment and scenario. If pasting this code into a console application, set values for the variables and paste the code into a class called from the Main function.

References to SourceCode.HostClientAPI, SourceCode.SmartObjects.Client and SourceCode.Framework are also required.

Copy

Scalar method example - loading a single record

public void ReturnSmartObjectDataUsingID() {

 // in this sample we define a string representing the ID of the SmartObject to use later
 string _id = "1";

 // TODO: Use the SCConnectionStringBuilder Class to construct a connection string
 string _connectionstring = "[K2environmentConnectionString]";

 // open a K2 Server connection
 SourceCode.SmartObjects.Client.SmartObjectClientServer serverName = new SmartObjectClientServer();
 serverName.CreateConnection();
 serverName.Connection.Open(_connectionstring.ToString());

 try {
  // get an object representing the SmartObject definition so we can set properties or interrogate the SmartObject definition
  SmartObject smartObject = serverName.GetSmartObject("Employee");

  // specify which method will be called. Here we're calling the load method.
  smartObject.MethodToExecute = "Load";

  // Here we set the value of the input parameter for the method. We're using the SmartObjects ID property and setting it to our defined string, _id.
  smartObject.Properties["ID"].Value = _id;

  /* use the ExecuteScalar method to use an execute type SmartObject method, Load in this case.
Set the input properties as shown above, and then use the ExecuteScalar() method to perform the SmartObject method. */
  serverName.ExecuteScalar(smartObject);

  // read the return properties of the SmartObject and write them to the console for example
  Console.WriteLine("First Name: " + smartObject.Properties["FirstName"].Value);
  Console.WriteLine("Last Name: " + smartObject.Properties["LastName"].Value);
  Console.WriteLine("Email: " + smartObject.Properties["Email"].Value);
  Console.ReadLine();
 } catch (Exception ex) {
  // write error to console
  Console.WriteLine("Error: " + ex.Message);
  Console.ReadLine();
 } finally {
  // finally, close the connection
  serverName.Connection.Close();
 }
}
Copy

List method example - loading multiple records

public void ReturnAllSmartObjectData() {
 // TODO: Use the SCConnectionStringBuilder Class to construct a connection string
 string _connectionstring = "[K2environmentConnectionString]";

 // open a K2 Server connection
 SourceCode.SmartObjects.Client.SmartObjectClientServer serverName = new SmartObjectClientServer();
 serverName.CreateConnection();
 serverName.Connection.Open(_connectionstring.ToString());

 try {
  // get an object representing the SmartObject definition so we can set properties or interrogate the SmartObject definition
  SmartObject smo = serverName.GetSmartObject("Employee");

  // specify which method will be called. Here we're calling the GetList method
  smo.MethodToExecute = "GetList";

  // execute the method and iterate over the resulting records reading the return properties of the SmartObject
  SmartObjectList smoList = serverName.ExecuteList(smo);
  foreach(SmartObject resultingSmO in smoList.SmartObjectsList) {
   Console.WriteLine("First Name: " + resultingSmO.Properties["FirstName"].Value);
   Console.WriteLine("Last Name: " + resultingSmO.Properties["LastName"].Value);
   Console.WriteLine("Email: " + resultingSmO.Properties["Email"].Value);
  }

  Console.ReadLine();
 } catch (Exception ex) {
  // write error to console
  Console.WriteLine("Error: " + ex.Message);
  Console.ReadLine();
 } finally {
  // close the connection
  serverName.Connection.Close();
 }
}

There may be times when retrieving a result set from a read SmartObject operation returns nothing, if this is possible in your scenario then you can use the IsEmpty property on the SmartObject class to check for null data.