Accessing customtable data from within a macro (Kentico v7)

Seamus Walsh asked on January 30, 2018 15:53

Greetings,

I'm using an old version of Kentico (v7, we'll be upgrading soon!).

I'm having problems accessing customtable data from within a macro. Below is a screenshot from the Macro Debug log, null items are returned.

I can confirm the customtable has data populated. Is this problem specific to v7 of the platform? Or am I trying to access it incorrectly?

Image Text

Recent Answers


Peter Mogilnitski answered on January 30, 2018 16:15 (last edited on March 24, 2018 12:16)

You doing everything right except the system doesn't how many rows you are going to get after "where":

{% GlobalObjects.CustomTables["customtable.SampleTable"].Items.Where("ItemID = 1")[0]["EmailText"] %}

ie. where("condition")[0]["fieldname"]

0 votesVote for this answer Mark as a Correct answer

Seamus Walsh answered on January 30, 2018 16:46

Still no luck unfortunately, I've been trying a few different permutations. The items value always appears to be null.

Image Text

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 30, 2018 16:58 (last edited on January 30, 2018 17:03)

"where" returns array with 1 item! i.e. you do [0] not [1]

You have to do .Items.Where("ItemID=1")[0]["EmailAddress"]

0 votesVote for this answer Mark as a Correct answer

Seamus Walsh answered on January 30, 2018 17:05 (last edited on January 30, 2018 17:05)

Still null. Shouldn't the Items debug value is something non-null?

Image Text

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 30, 2018 17:34 (last edited on March 24, 2018 12:16)

You probably misspelled table name. My code in the first comment is working. Try .Items.Count to see if you have anything in the table. If you just do: {%GlobalObjects.CustomTables["customtable.sampletable"].Items%} you should get some output if the table is not empty

0 votesVote for this answer Mark as a Correct answer

Seamus Walsh answered on January 30, 2018 18:09

Hmm. If I change name of table to something I know that is incorrect, it correctly returns null

I've tried Items.Count (and Length) but they both return null. The customtable is available to the site and the permissions look okay.

Not sure if this is possible in Kentico v7.

Image Text

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 30, 2018 18:27 (last edited on March 24, 2018 12:16)

I think you are getting the name of the table wrong. Is it a real custom table in Kentico? You should use codename i.e. Image Text All those macros Image Text

do return somehting:

Image Text

P.S. Make sure that this a custom table in SQL i.e.: Image Text

I think you just trying access some table in your DB , but it it not a customtable in Kentico sense.

If you want to access form table it will be a different macro: dont remember how it was in 7 but this will work in 11: {% GlobalObjects.BizForms["BizForm.TestForm"].Items.Count %}

0 votesVote for this answer Mark as a Correct answer

Seamus Walsh answered on January 30, 2018 18:41

I can access the table name but not the items. Very strange.

Image Text

And the data in the sample table:

Image Text

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 30, 2018 20:22

You've got to play with macros, there was macro tester I used to use, but you can do in the interface as well. Here you have access to macros Image Text

Get to custom tables I don't think .Items exist in 7. It has to be a way around it. In the worst case scenario you can create a custom macro and access data via API, but play around it has to be something there.

0 votesVote for this answer Mark as a Correct answer

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