Macro for getting field value from all rows of custom table.

Targutai Yesugei asked on September 1, 2017 08:11

Hello.

I wan't to create field in web part with custom table column values as source. So i need a macro to get appropriate list of strings from custom table.

I tried to find it myself, but all i could write is

GlobalObjects.CustomTables["namespace.mytablename"].Children.All.GetItem("ColumnName")#

And it's returns CMS.Modules.PermissionNameInfo (cms.classpermission) - Read which isn't what i need.

Thanks in advance.

Correct Answer

Chetan Sharma answered on September 1, 2017 16:06

You can easily play with this using System -> Console. It provides you a nice interface through to access all Kentico objects through wrappers. So I did this on my CT.

Table is BreakEvensData and Column is Start_Weight

CMSContext.Current.GlobalObjects.CustomTables["BreakevensData"].Items.ItemsAsFields.Start_Weight

I go this as result

550 650 750 850 300 500

I checked this in Data tab and result is verified.

Manipulate this and see if it works

Screenshot

2 votesVote for this answer Unmark Correct answer

Recent Answers


Michal Samuhel answered on September 1, 2017 08:29 (last edited on September 1, 2017 08:29)

H Targutai,

You are on right track now. You can use GlobalObjects.CustomTables["customtable.TestTable"].Items and it will return an InfoObjectCollection with all items and data.

But you will need to iterate over this collection to get items with macro foreach loop. Or you can use approach with applyTransformation macro which Kristian mentions on following article:

https://www.kenticotricks.com/blog/using-the-applytransformation-macro

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on September 1, 2017 11:24

Thank you Michal!

I tried following macro: z = ""; foreach (x in GlobalObjects.CustomTables["foo.tablecodename"].Items) {z += x.GetValue("ColumnName") += "|"}; z# but it also didn't work

I use macro without brackets beacause hint in data source editing tells me so.

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on September 4, 2017 13:19

Thanks a lot, Chetan!

0 votesVote for this answer Mark as a Correct answer

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