Skip to main content

There are tutorials that describe the process of creating and editing smartobject instances using asp.net and the sqldatasource component. These tutorials are easy to get going and demonstrate k2 interaction in a simple manner, but they are not an example of how to interact with smart objects via code.

I would like a straightforward example of how to create and edit a smartobject using code. You can assume the following:

  • A smartobject of type "Employee" has already been defined and successfully published to the k2 development server.
  • An ASP.NET page has been created and some input fields and a save button have been added.

I'm looking for something vaguely like this pseudocode:

private void SaveButton_Click(object sender, EventArgs e)

{

   Connection con = new Connection("k2devserver");

   SmartObject smartObj = new SmartObject(con, "Employee")

   smartObj.Propertiese"FirstName"].Value = FirstNameTextBox.Text;

   smartObj.Save();

}

I expect that the above pseudocode will demonstrate how limited my understanding of the k2 api is, but hopefully it illuminates the sort of thing I am trying to do.

Thanks. 

 

Hi,


 Here is some sample code that shows one way to interact with a smartobject using code:


//Insert Content into SmartObjects
SourceCode.Data.SmartObjectsClient.SOCommand soCommand = new SourceCode.Data.SmartObjectsClient.SOCommand();
SourceCode.Data.SmartObjectsClient.SOConnection soConn = new SourceCode.Data.SmartObjectsClient.SOConnection("Server=blackpearl;Port=5555;AutoAlias=False;SmartObjects=");
soCommand.Connection = soConn;
soCommand.CommandText = smartObjectName].Create;
soCommand.CommandType = CommandType.StoredProcedure;
//Clear any parameters
soCommand.Parameters.Clear();
//Set Parameters        
soCommand.Parameters.AddWithValue("Title", "My Title");
soCommand.Parameters.AddWithValue("Description", "My Description");
soCommand.Parameters.AddWithValue("Email", bob@somecompany.com);

//Execute the Command (No ADO.Net Provider needed)
object results = soCommand.ExecuteScalar();


 I hope this helps.


 


So the method that Eric posted (using SourceCode.Data.SmartObjectsClient) works, but that is a very ADO-centric way of working with SmartObjects. A simpler way, is to use the SourceCode.SmartObjects.Client assembly. Here is a code sample:


 


using SourceCode.SmartObjects.Client;
using SourceCode.Hosting.Client.BaseAPI;



//Create a SO Server Client Object
SmartObjectClientServer soServer = new SmartObjectClientServer();
SCConnectionStringBuilder cb = new SCConnectionStringBuilder();


//Build a SC Connection String
cb.Host = "localhost";
cb.Port = 5555;
cb.Integrated = true;
cb.IsPrimaryLogin = true;


//Open the connection to the K2 Server
soServer.CreateConnection();
soServer.Connection.Open(cb.ToString());


//Get a handle to the 'Employee' SO
SmartObject soEmployee = soServer.GetSmartObject("Employee");


//Call the Load Method
soEmployee.MethodToExecute = "Load";
soEmployee.Propertiese"ID"].Value = "1234";
soServer.ExecuteScalar(soEmployee);


//Read the Properties
string sFirstName = soEmployee.Propertiese"FirstName"].Value;
string sLastName = soEmployee.Propertiese"LastName"].Value;



//Example of calling the 'Save' Method
soEmployee.Propertiese"FirstName"].Value = sFirstName.ToUpper();
soEmployee.Propertiese"LastName"].Value = sLastName.ToUpper();
soEmployee.MethodToExecute = "Save";
soServer.ExecuteScalar(soEmployee);


//Close the Connection
soServer.Connection.Close();


Thank you Olaf, thats exactly what I was looking for. I had attempted to use the SmartObjects.Client assembly but struggled with the connection string (didnt notice the builder) and didn't realise the ExecuteScalar() was how to call the specified method.

Is there ANY documentation for the SourceCode.SmartObjects.Client assembly? The only API reference documentation I've been able to find so far is for SourceCode.Workflow.Client.

The next thing I want to do is return a list of SmartObjects that match a specified criteria i.e. a search. I figure one way to do it is using the SourceCode.Data.SmartObjectsClient assembly. Another way that sort of works is calling the GetList method on a SmartObject, but this approach does not seem flexible i.e. no support for wildcard search or partial match (specify GivenName as 'PA', does not return result with GivenName = 'PAUL'). Are there some better alternatives for searching?

Also while on the topic of searching, what alternatives are there if a SoundEx search is required?
 

Thanks,

Paul Batum
 


FYI - If you use Olaf's code from above make certain that you replace the spaces in the SmartObject name and MethodToExecute with underscores.


icon-quote.gifOlafWagner:

//Call the Load Method
soEmployee.MethodToExecute = "Load";
soEmployee.Properties["ID"].Value = "1234";
soServer.ExecuteScalar(soEmployee);


//Read the Properties
string sFirstName = soEmployee.Properties["FirstName"].Value;
string sLastName = soEmployee.Properties["LastName"].Value;



How would one to loop trough the results of the "Load" Method ?


Above sample probably returns one row. But what in the case that multiple results are returned ? (eg multiple FirstNames and LastNames)


Well the problem is that you are calling ExecuteScalar which I do not believe will return a collection.  If I were expecting a collection I would personally use the GetList method and call ExecuteList, something like below...


SmartObject employee = server.GetSmartObject("Employee");
employee.MethodToExecute = "Get_List";
SmartObjectList employees = server.ExecuteList(employee);


foreach (SmartObject so in employees.SmartObjectsList)
{
      string bar = so.Propertiesp"LastName"].Value.ToString().Trim() + " " +
      so.Propertieso"Phone_Number"].Value.ToString().Trim();
      Console.WriteLine(bar);
}


 Hope this helps,


Jason


Thanks ! Works perfectly! The MethodToExecute is "GetList" and not "Get_List" :-)


 


True, it is that by defualt. 


Although I have a composite SmartObject where I copied that code from.  Since the method name has a space in it "Get List", when I modified the SmartObject method to read from multiple smartobject service I had to put an underscore in the name as spaces in the method names will be replaced with an underscore underneath the hood.


I have another question about this.


I have to give my NT account rights on SmartObject Security Settings in order to let this work.


If I remove my NT account, and just give "Everyone" All Security rights on the SmartObject, it does not work.


I get the exception that Authorization failed.


The connection string looks like this:ConnectionString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=MYSERVER;Port=5555"


I think that Integrated must be false ? But how must the ConnectionString look in order to give the group "Everyone" the necessary rights ?


Thanks,


Nicolas


 


Reply