SmartObject Execute List Methods

The SmartObject ExecuteList methods are used to return one or more instances of a SmartObject. You need to instantiate the SmartObject you wish to run, set any properties and then issue the ExecuteList() method which will return a SmartObjectList (which is a collection of SmartObjects). You can then interact with list results in a collection or a reader pattern.

SmartObjectList Method

Return records as a collection of SmartObject objects. On the client for large result sets (e.g. >100 records), this operation can be memory-intensive.

Copy

SmartObjectList method

// 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());

// instantiate the SmartObject you wish to run
SmartObject mySmO = serverName.GetSmartObject("ADUser");

//call a list method to return a collection of records
//in this case filtered where Name = Joe
mySmO.Properties["Name"].Value = "Joe";
mySmO.MethodToExecute = "GetList";
SmartObjectList smoList = serverName.ExecuteList(mySmO);
foreach(SmartObject smoItem in smoList.SmartObjectsList) {
 //do something with the SmartObject
}

ExecuteListCSV Method

The ExecuteListCSV method will return a simple string for each SmartObject in the returned collection, separating each property with a comma. Blank values are returned as blank strings. Note that the SmartObjects themselves are also separated with a comma.

Copy

ExecuteListCSV method

// get an object representing the "ADUser" SmartObject definition which contains all the users in AD in this case
SmartObject smartObject = serverName.GetSmartObject("ADUser");

// Call the List_Users method and write the results to a CSV string
smartObject.MethodToExecute = "List_Users";
string resultsInCSV = serverName.ExecuteListCSV(smartObject);

// e.g. write the returned CSV string to a text file called ADUsers.txt
File.WriteAllText(@ "C:\Users\Public\TestFolder\ADUsers.txt", resultsInCSV);

ExecuteListReader Method

The SmartObjectReader object is a read-forward reader you can use in a while statement, as shown in the code sample below. This returns records in batches, not all at once, and is a quick and resource-efficient way to read through records.

Copy

ExecuteListReader method

// get an object representing the SmartObject definition so we can interrogate it. We're getting the Employee SmartObject here
SmartObject smartObject = serverName.GetSmartObject("Employee");

//use reader object to iterate list method results
SmartObjectReader K2Reader = serverName.ExecuteListReader(smartObject);

// for each row, write the data to console
while (K2Reader.Read()) {
 string propValue = K2Reader["PropertyName"].ToString();
 Console.WriteLine(propValue);
}

ExecuteListDataTable Method

This method is used to return a System.Data.DataTable object containing one or more instances of a SmartObject that you can then manipulate or read using standard System.Data operations. Each row in the table represents an instance of the SmartObject and the table will contain a column for each property returned by the SmartObject method.

Copy

ExecuteListDataTable method

//get an object representing the SmartObject definition so we can interrogate it. We're getting the Employee SmartObject here
SmartObject smartObject = serverName.GetSmartObject("Employee");

//return list method results as a DataTable
DataTable dataTable = serverName.ExecuteListDataTable(smartObject);

//you can now manipulate or read the DataTable using standard System.Data operations