Generating Forms and Views with C#
The following C# code samples show you how to generate a view based on an existing SmartObject, and a form containing existing views. Comments in the code samples explain each step.
Generate a view based on a pre-existing SmartObject
/// <summary>
/// This method will generate a view based on a pre-existing SmartObject in K2.
/// </summary>
public void GenerateView()
{
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder csbuilder = new SCConnectionStringBuilder("[CONNECTIONSTRING]"); //TODO: provide connection string for your environment
string soName = "[SMARTOBJECTNAME]"; //TODO: provide the system name for the SmartObject you want to generate a view for
string viewName = "[VIEWNAME]"; //TODO: provide the new view's desired name, e.g. "Expense Claim Detail Item View"
string categoryPath = "[K2CATEGORY]"; //TODO: provide the category where the view should be created, e.g. "Finance\Expense Claim\Forms"
SourceCode.Forms.Authoring.ViewType viewType = SourceCode.Forms.Authoring.ViewType.List; //TODO: specify the view type, e.g. ViewType.Capture, ViewType.CaptureList, ViewType.Content, or ViewType.List
//TODO: build up a collection of the properties you want to show on the view
System.Collections.Generic.List<string> soProperties = new List<string>() {
"PROPERTY1",
"PROPERTY2",
"PROPERTY3"
}; // e.g. ID, First Name, Start Date, Description, etc.
//TODO: build up a collection of the methods you want to enable on the view
System.Collections.Generic.List<string> soMethods = new List<string>() {
"Create",
"Save",
"Delete",
"Load",
"GetList"
};
//TODO: define the layout and behavior. Review ViewCreationOption Enum object for more options.
//NOTE: You must include ViewCreationOption.IsEditable for the Editable view types of Capture and CaptureList
SourceCode.Forms.Utilities.ViewCreationOption vcOptions = ViewCreationOption.IsEditable | ViewCreationOption.LabelsToLeftOfControls;
// FormsManager provides the connection to K2 to generate and deploy View
using (SourceCode.Forms.Management.FormsManager formsManager = new FormsManager())
{
//open the connection
formsManager.CreateConnection();
formsManager.Connection.Open(csbuilder.ConnectionString);
// Define an AutoGenerator object which will be used to physically generate a view in K2
using (SourceCode.Forms.Utilities.AutoGenerator autoGenerator = new AutoGenerator(formsManager.Connection))
{
// Define a ViewGenerator object using the ViewType and ViewCreationOption objects.
SourceCode.Forms.Utilities.ViewGenerator viewGenerator = new ViewGenerator(viewType, vcOptions);
switch (viewType)
{
// If viewType is one of the editable views, use "InputProperties" for viewGenerator
case SourceCode.Forms.Authoring.ViewType.CaptureList:
case SourceCode.Forms.Authoring.ViewType.Capture:
viewGenerator.InputProperties.AddRange(soProperties);
break;
// If viewType is one of the display views, use "DisplayProperties" for viewGenerator
case SourceCode.Forms.Authoring.ViewType.Content:
case SourceCode.Forms.Authoring.ViewType.List:
viewGenerator.DisplayProperties.AddRange(soProperties);
break;
}
//TODO: Set the default list method if your SmartObject has multiple list methods defined. e.g. "GetList"
viewGenerator.DefaultListMethod = "[LISTMETHODNAME]";
if (soMethods.Count > 0)
{
// Tell K2 which methods should have buttons visible on the new view.
viewGenerator.InstanceMethods.AddRange(soMethods);
}
// With the viewGenerator object, auto-generate the view based on the SmartOject in the system and give it a View name.
SourceCode.Forms.Authoring.View generatedView = autoGenerator.Generate(viewGenerator, soName, viewName);
// Deploy the view to K2 using its XML definition, making sure to pass in the category Path.
// NOTE: Set option to Check-In the view to true or false
formsManager.DeployViews(generatedView.ToXml(), categoryPath, false);
}
}
}
Generate a form containing pre-existing Views
/// <summary>
/// This method will generate a form containing pre-existing Views.
/// </summary>
public void GenerateForm()
{
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder csbuilder = new SCConnectionStringBuilder("[CONNECTIONSTRING]"); //TODO: provide connection string for your environment
string formName = "[SMARTFORMNAME]"; //TODO: Provide the name for the new form
string categoryPath = "[K2CATEGORY]"; //TODO: provide the category where the new form should be created, e.g. "Finance\Expense Claim\Forms"
string formTheme = "THEMENAME"; //TODO: provide the theme for the new form, e.g. Lithium, Platinum, SharePoint 2013, Blue Void, etc.
string[] viewNames = {
"VIEWNAME1",
"VIEWNAME2",
"VIEWNAME3"
}; //TODO: provide the names of the Views to include on the new Form
SourceCode.Forms.Authoring.View generatedView = new View();
//TODO: set up the Form options
SourceCode.Forms.Utilities.FormBehaviorOption fbOptions = FormBehaviorOption.LoadFormListClick | FormBehaviorOption.RefreshListFormLoad;
SourceCode.Forms.Utilities.FormGenerationOption fgOptions = FormGenerationOption.NoTabs;
// FormsManager provides the connection to K2 to generate and deploy the form
using (SourceCode.Forms.Management.FormsManager formsManager = new FormsManager())
{
//open the connection
formsManager.CreateConnection();
formsManager.Connection.Open(csbuilder.ConnectionString);
// Define an AutoGenerator object which will be used to generate the form
using (SourceCode.Forms.Utilities.AutoGenerator autoGenerator = new AutoGenerator(formsManager.Connection))
{
// Define a FormGenerator object using the FormGenerationOption, FormBehaviorOption objects and theme name.
SourceCode.Forms.Utilities.FormGenerator formGenerator = new FormGenerator(fgOptions, fbOptions, formTheme);
// Use AutoGenerator object to generate the Form object and pass in the names of views to add to the Form.
// NOTE: could also be a list of view GUIDs, see overload options for Generate() method.
SourceCode.Forms.Authoring.Form generatedForm = autoGenerator.Generate(formGenerator, viewNames, formName);
// Deploy the form to the K2 environment using the XML definition structure and a K2 category.
// NOTE: Set option to Check-In the form to true or false
formsManager.DeployForms(generatedForm.ToXml(), categoryPath, false);
}
}
}