K13 - Custom Data Table

Julian C. asked on December 13, 2022 23:15

Kentico 13.

I'm trying to display content from a custom data table with not much luck. Most of the examples I found shows only partial code.

Do you guys know of any good examples on how to display content from a custom data table?

I was able to retrieve some data but I have no control over it.

Here's what I have:

Controller: IEnumerable<TableTest1Item> tableContent = CustomTableItemProvider.GetItems<TableTest1Item>(); return PartialView("Widgets/_MyDataWidget", tableContent);

Partial View: @model IEnumerable<CMS.CustomTables.Types.Customtable.TableTest1Item>@foreach (var item in Model){<h4>@item.GetValue("Title")</h4><p>@item.GetValue("Description")</p>}

Ideal would be a widget that allows you to select any custom data table and apply a transformation like in good old days repeaters.

Thanks

Correct Answer

Brenden Kehren answered on December 22, 2022 15:14

Julian,

Widget properties will allow you to do this. In your properties, create some dropdowns with your custom table values (static or dynamic). Then in your controller, get the value the user selected and execute your code.

In your compenent viewmodel/controller do something like this:

if (!string.IsNullOrEmpty(viewModel.Properties.CustomTableName))
{
    // check to see if the custom table exists in the system.
    DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
    if (customTable != null)
    {        
        // this is where it gets interesting, you need to switch through the custom table object names to determine what DTO you'll call to get your strongly typed data back OR you can create a generic custom table item class that does the transformation of data from your custom table object to a generic object for each of the custom table classes (up to you). 
        switch (customTable.ClassName.ToLower())
        {
            case "customtable.Table1":          
                // call your DTO for the customtable.Table1 object
                var ct1Model = CustomTable1ViewModel.GetViewModel(customTableRepo.GetCustomTable1Items(customTable.ClassName));
                return View("~/Components/Widgets/CustomTableItems/_CustomTable1View.cshtml", ct1Model);
                case "customtable.Table2":          
                // call your DTO for the customtable.Table2 object
                var ct1Model = CustomTable2ViewModel.GetViewModel(customTableRepo.GetCustomTable2Items(customTable.ClassName));
                return View("~/Components/Widgets/CustomTableItems/_CustomTable2View.cshtml", ct1Model);
        }
    }
}

While this is somewhat of a basic approach, I'm sure others could come up with a more complex/typed approach where you wouldn't have to add code everytime you added a custom table object in the UI.

Note: I typed this by hand in the editor so it may not be valid C#/API calls but you should get the idea.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Ei Dar Li Ko Ko answered on December 14, 2022 07:56

I have same issue want to retrieve the custom table data and view back inside the widget.

0 votesVote for this answer Mark as a Correct answer

Not Applicable answered on December 15, 2022 08:09

You can use a where condition when retrieving data to filter the results, see Loading data records from a custom table in the API examples. For example, put that in a repository class and call it from your widget ViewComponent.

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on December 15, 2022 19:52

Thanks Marcel, but I was hoping to get a little bit more than a simple example which is not really useful for a newbie.

It's becoming very frustrating to find useful code examples for Kentico 13.

From a car perspective, Kentico sells you the car but offers you only the engine, and demands you to build the rest. Not fair and I don't see how this model will benefit its clients. Very frustrating, did I mention that?

0 votesVote for this answer Mark as a Correct answer

Julian C. answered on December 27, 2022 20:41

Brenden, thank you for your answer. It makes total sense but I'm still looking for a better way to retrieve content from custom data tables.

I have about 30 custom data tables and hard-coding the table names and using so many switch/if statements seems like overkill.

I'm surprised nobody created a widget that handles custom data tables. I've seen some for pages and media library but not for custom data tables.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 27, 2022 21:13

Take what I provided and make it more dynamic. Make your naming conventions the same for your custom tables, views, etc. and you can essentially remove the switch and the rest of the code.

The above is the easy part. Where it will come in difficult is when you render the view. Unless you have a generic model for all possible custom tables, you'll need to do some customization of those views.

It's been done, I can guarantee the people who have done it have spent a lot of time to make it as dynamic as possible and their time comes with a cost. Why hasn't Kentico done it, well because, their time comes with a cost and if there isn't a huge need or request for it then it will be low priority on the roadmap.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.