Here’s some sample code for working with a SmartObject

http://jeylabsblog.com/2007/09/18/code-manipulation-of-smart-objects/

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;

try
{
    socSvr.Connection.Open(connectionStrBuilder.connectionString);
    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);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s