Recently I encountered an issue while deploying a package from one environment to another. While testing the smartobject after the deployment, the following error message pop up :
K2 SmartObject can read and store files into SQL table via its File property. Files to be stored into SQL table will be automatically converted to base64 encoded string and when retrieved from SQL, it will be automatically converted back to binary.
In this article, we will see how to setup and use SmartObject upload file to SQL and test the function in K2 smartforms.
Use SmartObject upload file to SQL
We start by setting up the SQL table. The column that will be storing the file content will be created as a nvarchar(max) type.
If the Service Instance to the SQL database (in this case “FileUploadTest”) has not been created yet, create it. If the Service Instance already exists, refresh the Service Instance. Make sure the new table and its properties are available.
Right-click on the [dbo].[FileUpload] Service Object and select Create SmartObject.
Give the new SmartObject an appropriate Name and click on Publish SmartObject.
You should find the SmartObject at the Category it is created above.
Next, find the SmartObject in K2 Designer and Edit it.
Select the property that is supposed to hold the file content (i.e. fileContent in this case) and click on the Edit button.
Edit the SmartObject’s fileContent property
Now, this is the most important step. Change the Type to File and click OK.
Lastly, Click Finish to complete editing the SmartObject and you are done.
Testing
Now, to test the SmartObject, we will use the Generate View feature. Right-click on the SmartObject and select Generate Views.
Select the checkbox under Item and click OK. This will generate an Item View.
Select the newly created Item View and click on Run.
In the form, fill in the file Name, select a file in file Content field and click Create. This will create the record in SQL.
Take note of the generated ID field value and check against the SQL table created earlier. You will find the newly created record with the file content.
To verify that the file was indeed saved successfully into the table, refresh the View used earlier, fill in the ID field value and click on Read button to load the file. Click on the file to download it and make sure it can be open in the appropriate application.
And that’s all on how to use SmartObject to upload file to SQL.
If you need to pass NULL to SmartObject properties or input parameters, use the string “scnull”. This will be parsed as NULL when the Service Broker translate your request.