Create List using ListDefinition
- Last UpdatedApr 02, 2024
- 5 minute read
Create a list using the list API.
The list API makes use of the following DLL references, namespaces, classes, and methods.
|
DLL Reference |
Namespace |
|---|---|
|
|
|
Class |
|---|
|
|
Methods |
|---|
|
Scenario
Create an Employee Details list mapped to an Employee table with the fields such as Title, Name, Age, Salary, Department, and Manager by running the following code.
CREATE TABLE [dbo].[Employee](
[Id] [uniqueidentifier] NOT NULL CONSTRAINT [DF__Employee__Id__1E6F845E] DEFAULT (newid()),
[Title] [nvarchar](250) NULL,
[Name] [varchar](50) NULL,
[Age] [int] NULL,
[Salary] [money] NULL,
[Department] [varchar](50) NULL,
[Manager] [varchar](50) NULL,
[Application] [nvarchar](150) NULL,
[CreatedBy] [uniqueidentifier] NULL,
[CreatedDateTime] [datetime] NULL,
[LastUpdatedDateTime] [datetime] NOT NULL,
[WIP] [bit] NULL,
[WIPItemId] [uniqueidentifier] NULL,
[MainItemId] [uniqueidentifier] NULL,
[ParentItemId] [uniqueidentifier] NULL,
[RecordParentItemId] [uniqueidentifier] NULL,
[TableId] [uniqueidentifier] NULL,
[ItemRowIndex] [int] NULL,
[IsLatest] [bit] NULL,
[Version] [nvarchar](25) NULL,
[VersionHistory] [nvarchar](max) NULL,
[Status] [int] NULL,
[LockedBy] [uniqueidentifier] NULL,
[LockedOn] [datetime] NULL,
[ListID] [uniqueidentifier] NULL,
[SecuritySettings] [nvarchar](max) NULL,
[ItemId] [uniqueidentifier] NULL,
[ItemType] [int] NULL,
[SecurityItemId] [uniqueidentifier] NULL,
[Owner] [uniqueidentifier] NULL,
[ModifiedBy] [uniqueidentifier] NULL,
[AuditTrail] [nvarchar](max) NULL CONSTRAINT [DF_Employee_AuditTrail] DEFAULT (''),
[IsDisabled] [bit] NULL CONSTRAINT [DF_Employee_IsDisabled] DEFAULT ((0)),
[WorkflowStatus] [varchar](50) NULL,
[SecurityCustomizationId] [uniqueidentifier] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_AMI_Employee] ON [dbo].[Employee]
(
[Application] ASC,
[MainItemId] ASC,
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
Go
if Not exists (select * from SKListTables where Application='<RepositoryName>' And TableName='Employee')
Insert into SKListTables (Application,TableName) Values ('<RepositoryName>','Employee')
Go
using System;
using Skelta.Repository.List;
using Skelta.Forms;
using Skelta.Forms.Core.Controls;
using Skelta.Core;
using Skelta.Repository.List.Action;
using System.Data;
namespace ListObject
{
public class DynamicList
{
static void Main(string[] args)
{
DynamicList DL = new DynamicList();
DL.CreateDynamicList("SkeltaRepo", "admin", "Employee", "Employee", "Employee");
}
public Guid CreateDynamicList(string applicationName, string LoggedInUser, string listName, string listDescription, string tableName)
{
// Deleting the list if it already exists.
ListDefinition _List = ListDefinition.GetList(new Skelta.Core.ApplicationObject(applicationName), "Manage List");
ListItem item = new Skelta.Repository.List.ListItem(_List, "<SkeltaDLList><EnableFolderSupport>True</EnableFolderSupport><Description></Description><Title>" + listName + "</Title></SkeltaDLList>");
item.LoggedInUserId = LoggedInUser;
item.Save(ListItemVersionStatus.Published);
ListDefinition newList;
newList = new ListDefinition(new Skelta.Core.ApplicationObject(applicationName));
newList.Definition = new BaseForm();
newList.Name = listName;
newList.ImageFileName = "skelta-DLItem-Item.png";
newList.Definition = GetListDefinition(listName, listDescription, tableName);
// Adding actions like new, edit, delete and so on.
AddActions(newList, listName);
newList.Save();
// Adding list to the navigation menu.
InsertListNavigation(applicationName, LoggedInUser, listName);
// Generating form.
// If you want to associate the list to an existing form, then the next two lines are not required. Also update formItemId with the existing form ID.
Skelta.Forms.Web.Common.ListFormDesignerFunctions lstFrmFns = new Skelta.Forms.Web.Common.ListFormDesignerFunctions();
Guid formItemId = lstFrmFns.SaveFormDefinition(applicationName, LoggedInUser, listName);
// Associating the list with the form.
Skelta.Forms.Web.Common.DynamicListCommonFunctions dlCmnFns = new Skelta.Forms.Web.Common.DynamicListCommonFunctions();
dlCmnFns.AddListFormAssociation(new Skelta.Core.ApplicationObject(applicationName), listName, newList.Id, formItemId);
return newList.Id;
}
// Creating ListMainForm for adding new Employees.
private ListMainForm GetListDefinition(string listName, string listDescription, string tableName)
{
ListMainForm mainForm = new ListMainForm();
mainForm.IsTableVersioned = false;
mainForm.Id = "_sys_" + listName.ToLower() + "_list" + "_skeltadynamiclist";
mainForm.UniqueTitle = false;
mainForm.XmlNodeBoundTo = listName;
mainForm.Name = listName;
mainForm.IsFolderSupported = true;
mainForm.TableName = tableName;
mainForm.Description = listDescription;
ListTextDataItem txtInput = new ListTextDataItem();
txtInput.Name = "Title";
txtInput.Id = "_sys_ListForms_" + txtInput.Name + Guid.NewGuid().ToString("N");
txtInput.GridCaption = "Title";
txtInput.BoundToTableField = "Title";
txtInput.BoundToTableFieldType = typeof(string);
txtInput.XmlNodeBoundTo = "Title";
txtInput.DisplayInLookup = true;
txtInput.IsLookupTitle = true;
txtInput.GridDisplayWidth = 150;
txtInput.GridDisplayIndex = 100;
txtInput.AllowSorting = true;
mainForm.Controls.Add(txtInput);
txtInput = new ListTextDataItem();
txtInput.Name = "Name";
txtInput.Id = "_sys_ListForms_" + txtInput.Name + Guid.NewGuid().ToString("N");
txtInput.GridCaption = "Name";
txtInput.BoundToTableField = "Name";
txtInput.BoundToTableFieldType = typeof(string);
txtInput.XmlNodeBoundTo = "Name";
txtInput.DisplayInLookup = true;
txtInput.IsLookupTitle = true;
txtInput.GridDisplayWidth = 150;
txtInput.GridDisplayIndex = 100;
txtInput.AllowSorting = true;
mainForm.Controls.Add(txtInput);
ListIntDataItem intInput = new ListIntDataItem();
intInput.Name = "Age";
intInput.Id = "_sys_ListForms_" + intInput.Name + Guid.NewGuid().ToString("N");
intInput.GridCaption = "Age";
intInput.BoundToTableField = "Age";
intInput.BoundToTableFieldType = typeof(int);
intInput.XmlNodeBoundTo = "Age";
intInput.IsLookupTitle = true;
intInput.GridDisplayWidth = 150;
intInput.GridDisplayIndex = 100;
intInput.AllowSorting = true;
mainForm.Controls.Add(intInput);
intInput = new ListIntDataItem();
intInput.Name = "Salary";
intInput.Id = "_sys_ListForms_" + intInput.Name + Guid.NewGuid().ToString("N");
intInput.GridCaption = "Salary";
intInput.BoundToTableField = "Salary";
intInput.BoundToTableFieldType = typeof(int);
intInput.XmlNodeBoundTo = "Salary";
intInput.IsLookupTitle = true;
intInput.GridDisplayWidth = 150;
intInput.GridDisplayIndex = 100;
intInput.AllowSorting = true;
mainForm.Controls.Add(intInput);
txtInput = new ListTextDataItem();
txtInput.Name = "Department";
txtInput.Id = "_sys_ListForms_" + txtInput.Name + Guid.NewGuid().ToString("N");
txtInput.GridCaption = "Department";
txtInput.BoundToTableField = "Department";
txtInput.BoundToTableFieldType = typeof(string);
txtInput.XmlNodeBoundTo = "Department";
txtInput.DisplayInLookup = true;
txtInput.IsLookupTitle = true;
txtInput.GridDisplayWidth = 150;
txtInput.GridDisplayIndex = 100;
txtInput.AllowSorting = true;
mainForm.Controls.Add(txtInput);
txtInput = new ListTextDataItem();
txtInput.Name = "Manager";
txtInput.Id = "_sys_ListForms_" + txtInput.Name + Guid.NewGuid().ToString("N");
txtInput.GridCaption = "Manager";
txtInput.BoundToTableField = "Manager";
txtInput.BoundToTableFieldType = typeof(string);
txtInput.XmlNodeBoundTo = "Manager";
txtInput.DisplayInLookup = true;
txtInput.IsLookupTitle = true;
txtInput.GridDisplayWidth = 150;
txtInput.GridDisplayIndex = 100;
txtInput.AllowSorting = true;
mainForm.Controls.Add(txtInput);
return mainForm;
}
private void AddActions(ListDefinition newList, string listName)
{
ListASPXUIAction newListItem = new ListASPXUIAction();
newListItem.Id = "_sys_new";
newListItem.Name = "New " + listName;
newListItem.ShowWhenSecurityAllowsRights = "";
newListItem.ValidOnlyForItemStatus = "";
newListItem.ValidOnlyForItemTypes = "1";
newListItem.PagePath = "DynamicListNewForm.aspx";
newListItem.Type = ListActionType.New;
newListItem.IconPath = "new";
newListItem.Top = 100;
newListItem.Left = 100;
newListItem.ValidForDisplayInGrid = ValidForDisplayGrid.MainGrid;
newListItem.WindowType = ListUIActionWindowTypes.Popup;
newListItem.WindowPosition = ListUIActionWindowPosition.Center;
newListItem.WindowSize = ListUIActionWindowSize.ScreenPropotional;
newListItem.DisplayValidRightValues = "ActionCreate<True>";
newListItem.ActionAllowValidRightValues = "ActionCreate<True>";
newListItem.ApprovalRequiredValidRightValues = "";
newListItem.ApprovalRequiredWithPromptValidRightValues = "";
newList.Operations.Actions.Add(newListItem);
ListASPXUIAction editForm = new ListASPXUIAction();
editForm.Id = "_sys_edit";
editForm.Name = "Edit";
editForm.WindowType = ListUIActionWindowTypes.Popup;
editForm.ShowWhenSecurityAllowsRights = "";
editForm.Type = ListActionType.MenuItem;
editForm.ValidOnlyForItemTypes = "";
editForm.ValidOnlyForItemStatus = "";
editForm.IconPath = "designerView";
editForm.PagePath = "DynamicListNewForm.aspx?mode=Edit";
editForm.DisplayValidRightValues = "ActionEdit<true:,owneditemsonly:>";
editForm.ActionAllowValidRightValues = "ActionEdit<true:,owneditemsonly:>";
editForm.ApprovalRequiredValidRightValues = "";
editForm.ApprovalRequiredWithPromptValidRightValues = "";
newList.Operations.Actions.Add(editForm);
ListASPXUIAction viewForm = new ListASPXUIAction();
viewForm.Id = "_sys_view_item_details";
viewForm.Name = "View Item Details";
viewForm.WindowType = ListUIActionWindowTypes.Popup;
viewForm.ShowWhenSecurityAllowsRights = "";
viewForm.Type = ListActionType.MenuItem;
viewForm.ValidOnlyForItemTypes = "";
viewForm.ValidOnlyForItemStatus = "";
viewForm.IconPath = "viewProperties";
viewForm.PagePath = "DynamicListNewForm.aspx?mode=View";
viewForm.DisplayValidRightValues = "ActionViewItemDetails<true:,owneditemsonly:>";
viewForm.ActionAllowValidRightValues = "ActionViewItemDetails<true:,owneditemsonly:>";
viewForm.ApprovalRequiredValidRightValues = "";
viewForm.ApprovalRequiredWithPromptValidRightValues = "";
newList.Operations.Actions.Add(viewForm);
ActionDelete deleteItem = new ActionDelete();
deleteItem.Id = "_sys_delete";
deleteItem.Name = "Delete";
deleteItem.ActionCommand = "_sys_delete";
deleteItem.ShowWhenSecurityAllowsRights = "";
deleteItem.Type = ListActionType.MenuItem;
deleteItem.ExecutionType = ActionExecutionType.SystemAction;
deleteItem.ValidOnlyForItemTypes = "";
deleteItem.ValidOnlyForItemStatus = "";
deleteItem.IconPath = "Delete";
deleteItem.DisplayValidRightValues = "ActionDelete<allow::,approval::,approvalprompt::>";
deleteItem.ActionAllowValidRightValues = "ActionDelete<allow::>";
deleteItem.ApprovalRequiredValidRightValues = "ActionDelete<approval::>";
deleteItem.ApprovalRequiredWithPromptValidRightValues = "ActionDelete<approvalprompt::>";
newList.Operations.Actions.Add(deleteItem);
ActionNewCategory newCategory = new ActionNewCategory();
newCategory.Id = "_sys_newfolder";
newCategory.Name = "ecm_list_cm_newcategory";
newCategory.ShowWhenSecurityAllowsRights = "";
newCategory.ValidOnlyForItemStatus = "";
newCategory.PagePath = "NewFormCategory.aspx";
newCategory.Left = 100;
newCategory.Top = 100;
newCategory.Width = 1000;
newCategory.ItemType = 0;
newCategory.Type = ListActionType.Task;
newCategory.Title = "New Category";
newCategory.WindowType = ListUIActionWindowTypes.Popup;
newCategory.WindowPosition = ListUIActionWindowPosition.Center;
newCategory.WindowSize = ListUIActionWindowSize.ScreenPropotional;
newCategory.DisplayValidRightValues = "ActionCreateFolder<true>";
newCategory.ActionAllowValidRightValues = "ActionCreateFolder<true>";
newCategory.ApprovalRequiredValidRightValues = "";
newCategory.ApprovalRequiredWithPromptValidRightValues = "";
newList.Operations.Actions.Add(newCategory);
ListASPXUIAction manageitemsecurity = new ListASPXUIAction();
manageitemsecurity.Id = "_sys_security_settings";
manageitemsecurity.Name = "Security Settings";
manageitemsecurity.ShowWhenSecurityAllowsRights = "";
manageitemsecurity.ValidOnlyForItemStatus = "";
manageitemsecurity.Type = ListActionType.MenuItem;
manageitemsecurity.IconPath = "settingsMSG";
manageitemsecurity.PagePath = "ListItemSettings.aspx";
manageitemsecurity.IsModal = false;
manageitemsecurity.ItemType = 1;
manageitemsecurity.WindowPosition = ListUIActionWindowPosition.Center;
manageitemsecurity.DisplayValidRightValues = "ManageSecurity<True>";
manageitemsecurity.ActionAllowValidRightValues = "ManageSecurity<True>";
manageitemsecurity.ApprovalRequiredValidRightValues = "";
manageitemsecurity.ApprovalRequiredWithPromptValidRightValues = "";
newList.Operations.Actions.Add(manageitemsecurity);
ListEventHandlerProvider eventProvider = new ListEventHandlerProvider();
eventProvider.ClassName = " Skelta.Repository.List.ListEventHandler";
eventProvider.AssemblyName = "Workflow.NET.NET2.dll";
eventProvider.Name = "_sys_" + listName.ToLowerInvariant() + "_listhandler";
newList.Operations.EventHandlers.Add(eventProvider);
}
private void InsertListNavigation(string applicationName, string loggedInUser, string listName)
{
Guid DLNavgnDtlsId = Guid.Empty;
ListTableCollection listCollection = new ListTableCollection(new Skelta.Core.ApplicationObject(applicationName), "SKENavigationDetails", "Id");
Skelta.Core.DS.SkeltaParameter paramTitle = new Skelta.Core.DS.SkeltaParameter("@Title", "ec_menu_Lists");
DataSet ds = listCollection.GetMatchingRecords(" And Title=@Title", paramTitle);
if (ds.Tables[0].Rows.Count > 0)
DLNavgnDtlsId = new Guid(ds.Tables[0].Rows[0][0].ToString());
else
DLNavgnDtlsId = Skelta.Forms.Web.Common.DynamicListCommonFunctions.InsertIntoListNavigation(applicationName, loggedInUser, "ec_menu_Lists", "ec_menu_Lists", null, "icon-lists.gif");
ds.Dispose();
ListDefinition listdef = ListDefinition.GetList(new Skelta.Core.ApplicationObject(applicationName), "NavigationDetails");
Skelta.Repository.List.ListItem item = new Skelta.Repository.List.ListItem(listdef);
((ListTextDataItem)item.ListForm.Records[0].FindControlByID("_sys_menu_disptext")).Value = listName;//"ec_menu_" + _listName;
((ListTextDataItem)item.ListForm.Records[0].FindControlByID("_sys_menu_desc")).Value = listName;//"ec_menu_" + _listName;
((ListTextDataItem)item.ListForm.Records[0].FindControlByID("_sys_menu_navto")).Value = "DynamicList.aspx?ListName=" + listName;
item.LoggedInUserId = loggedInUser;
item.ParentItemId = DLNavgnDtlsId;
item.Save(ListItemVersionStatus.Published);
}
}
}
Note: Ensure to replace all <RepositoryName> instances with your repository name.