Retrieve an IEnumerable<string> from custom table via C# api

Targutai Yesugei asked on September 26, 2018 14:37

Hello!

Need to retrieve an IEnumerable<string> of custom table's column values. I'm using

CustomTableItemProvider.GetItems("CLASSNAME")

to retrieve all rows from table.
Now I need to get specific column values list.

I see that there is a SelectColumnsList) property which probably can help me, but the problem is that i can't imagine how to use it, i tried to google and kentico doc search but it didn't help.

Thank you in advance.

Recent Answers


Brenden Kehren answered on September 26, 2018 14:49

.GetItems("className") is enumerable. It returns a ObjectQuery of generic CustomTableItem. So you need to loop through each of those records in order to get a specific record's column values. Then when you're in that record, you loop through the column values. See example below:

var tableItems = CMS.CustomTables.CustomTableItemProvider.GetItems("ClassName");
foreach (CMS.CustomTables.CustomTableItem cti in tableItems)
{
    string values = "";
    foreach (string columnName in cti.ColumnNames)
    {
        values += cti.GetStringValue(columnName, "");
    }
}
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on September 26, 2018 15:21 (last edited on September 26, 2018 15:34)

You can convert it to data set, I guess you will be more comfortable with it.

DataSet myDataSet = CustomTableItemProvider.GetItems("TableClassName")
                    .Columns("ColumnName1", "ColumnName2")
                    .Where("somecondition")
                    .Result;
IEnumerable<DataRow> rows = myDataSet.Tables[0].AsEnumerable();`

P.S. Just in case Custom Table API

0 votesVote for this answer Mark as a Correct answer

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