Creating and Editing SmartObjects

Last post 11-13-2007, 10:01 AM by NS. 9 replies.
Sort Posts: Previous Next
  •  09-12-2007, 10:26 AM 18568

    Creating and Editing SmartObjects

    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.Properties["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. 

     

  •  09-12-2007, 10:56 AM 18570 in reply to 18568

    Re: Creating and Editing SmartObjects

    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.


    The statements and opinions made in my postings are my own, and do not reflect the opinions of SourceCode Technology Holdings, Inc. or its subsidiaries. All information is provided as is with no warranties, express or implied, and grants no rights or licenses.
  •  09-12-2007, 11:54 AM 18574 in reply to 18570

    Re: Creating and Editing SmartObjects

     

    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.Properties["ID"].Value = "1234";
    soServer.ExecuteScalar(soEmployee);

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


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

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

  •  09-13-2007, 7:38 AM 18605 in reply to 18574

    Re: Creating and Editing SmartObjects

    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
     

  •  09-21-2007, 10:25 AM 18797 in reply to 18605

    Re: Creating and Editing SmartObjects

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

  •  11-12-2007, 8:25 AM 19759 in reply to 18574

    • NS is not online. Last active: 05-11-2008, 12:15 AM NS
    • Top 75 Contributor
    • Joined on 06-05-2007
    • Wielsbeke, Belgium
    • Posts 60
    • Points 155

    Re: Creating and Editing SmartObjects

    OlafWagner:

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

  •  11-12-2007, 9:43 PM 19768 in reply to 19759

    Re: Creating and Editing SmartObjects

    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.Properties["LastName"].Value.ToString().Trim() + " " +
          so.Properties["Phone_Number"].Value.ToString().Trim();
          Console.WriteLine(bar);
    }

     Hope this helps,

    Jason

  •  11-13-2007, 9:36 AM 19784 in reply to 19768

    • NS is not online. Last active: 05-11-2008, 12:15 AM NS
    • Top 75 Contributor
    • Joined on 06-05-2007
    • Wielsbeke, Belgium
    • Posts 60
    • Points 155

    Re: Creating and Editing SmartObjects

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

     

  •  11-13-2007, 9:41 AM 19786 in reply to 19784

    Re: Creating and Editing SmartObjects

    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.

  •  11-13-2007, 10:01 AM 19788 in reply to 19786

    • NS is not online. Last active: 05-11-2008, 12:15 AM NS
    • Top 75 Contributor
    • Joined on 06-05-2007
    • Wielsbeke, Belgium
    • Posts 60
    • Points 155

    Re: Creating and Editing SmartObjects

    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

     

View as RSS news feed in XML