Here’s some sample code for working with a…

Posted by

Here’s some sample code for working with a SmartObject:

We use the following namespaces from these DLLs (They are in the GAC as well)

using SourceCode.SmartObjects.Client; (DLL: SourceCode.SmartObjects.Client)
using SourceCode.Hosting.Client.BaseAPI; (DLL: SourceCode.HostClient)

//this is your starting point

SmartObjectClientServer socSvr = new SmartObjectClientServer();

//we now have a connection string builder helper

SCConnectionStringBuilder connectionStrBuilder= new SCConnectionStringBuilder();

socSvr .Connection = socSvr.CreateConnection();

connectionStr.Host = “servernameFromConfig”;

connectionStr.Integrated = true;

connectionStr.IsPrimaryLogin = true;

connectionStr.Port = portNumberFromConfig;




SmartObject so = socSvr.GetSmartObject(“MySmartObject”);

so.MethodToExecute = “Load”;

so.Properties[“prop1”].Value = “value”;

//add more props

//you can call the Save, Delete, etc methods (scalar methods) like this

so = socSvr.ExecuteScalar(so);

//you call the list method which retrurs a SmartObjectList like this

//SmartObjectList soList = socSvr.ExecuteList(so);


//this is a K2 Exception Class with useful properties
catch (SmartObjectException soe)

StringBuilder errorMessage = new StringBuilder();

foreach (SmartObjectExceptionData smartobjectExceptionData in soe.BrokerData)
string message = smartobjectExceptionData.Message;
string service = smartobjectExceptionData.ServiceName;
string serviceGuid = smartobjectExceptionData.ServiceGuid;
string severity = smartobjectExceptionData.Severity.ToString();
string innermessage = smartobjectExceptionData.InnerExceptionMessage;

errorMessage.AppendLine(“Service: ” + service);
errorMessage.AppendLine(“Service Guid: ” + serviceGuid);
errorMessage.AppendLine(“Severity: ” + severity);
errorMessage.AppendLine(“Error Message: ” + message);
errorMessage.AppendLine(“InnerException Message: ” + innermessage);



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s