Create a SmartObject From a Stored Procedure Using Visual Studio

One of the greatest things about my position is that I get to learn from the very best in this field.

I’m used to using K2’s tools — K2 Studio and K2 Designer. I’m used to consuming SmartObjects via the Object Browser in K2 Studio or from within the Designer. Those SmartObjects are generally enveloped within some sort of category structure.

Say you have a stored procedure you’d like to expose as a SmartObject. Let’s do that using K2 for Visual Studio instead of K2’s native tools.

(Before continuing, ensure you have a copy of Visual Studio, with K2 for Visual Studio and configured for your development K2 and a database environment with stored procedures.)

Create a K2 SmartObject project in Visual Studio, and create a new SmartObject.

Category Structure

Before you get into the guts of the build, use solution explorer to add folders to mimic the category structure you already have in place. So if you have a structure like Program > Project > SmartObjects, create three folders and nest them accordingly. Then take the project file and drag it up to the same level as the solution, so it and the top level folder are in vertical alignment.

When this deploys, the SET_DATA SmO will land in Program > Project > SmartObjects.

It might not make sense visually, but your SmartObjects should end up in the right spot.

The SmartObject Form

The main form features a region at the top with some basic property fields, a collapsible region in the center of the form for the SmartObject properties, and a collapsible region at the bottom third of the form for the methods. The object has five methods in place by default.

Create a name and description for your new object in the properties fields at the top.

Above those fields is a navigation bar with three buttons — the leftmost of them toggles the form between Advanced Mode and Simple Mode. Click Advanced Mode to expose navigational controls for the properties and methods regions. Next, navigate to the SmartObject Methods region and click Remove All to erase the default methods. Click OK on the warning dialog.

SmartObject Properties

Now the real work begins. In the SmartObject Properties region, click Add to bring up the Add SmartObject Properties dialog. The Add Property function adds a new SmartObject property and specifies the property’s details. Properties other than SmartBox properties should be linked to a service object method (they will, later).

Before you do anything else in that dialog, open up the SmartObjects Services Tester and find your stored procedure. Go into the ServiceObject Explorer and navigate the tree to your service object, then down to your procedure. Your procedure should appear as having no properties and only an execute method. DON’T FREAK BECAUSE YOU DON’T SEE ANY PROPERTIES. Open the parameters of the execute method. Notice they’re listed by name and data type. (Think it through: it’s a stored procedure — not a SmartObject yet, so it’s only method will be execute. The parameters are inside the method.)

Back in the Add SmartObject Properties dialog, Click to expose a new edit row. Enter the name, description, the data type, and check the box if this property is the primary key for this table. Transcribe the data you see in the SmartObject Services Tester to ensure everything matches up — a mismatched data type can cause a lot of headaches! Click OK to close the dialog and return to the main form. You should see the SmartObject Properties region is now populated with the data you entered.

Save your progress.

SmartObject Methods

In the SmartObject Methods region, click Add to bring up the SmartObject Method Wizard. The wizard will help you configure adding the new methods to the SmartObject, and in mapping Service Object methods to the SmartObject method.

Click Next>> to move to the Method Details panel. Here you can give your method a name and description, and select a type (think CRUD methods) and choose an action to perform in case your method is called as part of a database transaction. (For example, if you’re performing an update method, you might consider selecting the Rollback option.)

Click Next>> to move to the Add Service Object Method panel. Use the browser control to open a Context Browser and navigate the Service Object Server to your stored procedure, and into that Execute method. Drag that Execute method to the Service Object Method field on the form. You should see the input parameters populate with the parameters from your procedure.  At this point, you need to map the input parameters to the properties — an easy exercise, but it can get a little monotonous if you have a lot of properties.

Click Finish to close the wizard. The SmartObject Methods region should populate with details of each method you create. Repeat the process for other methods you intend to create.

Save your progress. Ready to build? Yes you are.

Build and Deploy

Up in Visual Studio’s Solution Explorer, right-click on your solution and select Build. You can watch the status in the Output window, which should appear from the bottom of the window. Hopefully, you should quickly see a message that reads “Build: 1 succeeded or up-to-date, 0 failed, 0 skipped.

Let’s deploy it.

From the same place where we found the build command, click Deploy to launch the Deploy Project Wizard, which we should recognize from K2 Studio. Once you’ve pulled the trigger, you’ll see the Output window again, recounting the exercise.

Consider opening up K2 Designer and looking for your SmartObject. Hopefully it’s nestled snugly within the category structure you replicated within Visual Studio. If it isn’t, fixing it is no big deal — right click on the object and select Move To from the context menu. Navigate the category structure to the correct placement, and click OK to affect the move.