Use macro expression in whereCondition

Bo Fu asked on June 4, 2015 18:02

Hi,

I am trying to use macro expression in whereCondition. The whole query is like "SELECT ... From View_CMS_Tree_Joined WHERE {%MyCustomMacro(NodeGUID)%} = '...'". Instead of getting NodeGUID from View_CMS_Tree_Joined it seems I got the currentDocument.NodeGUID. So is there any way to get NodeGUID from the view? Thanks

Correct Answer

Brenden Kehren answered on June 5, 2015 14:24

A better solution would be to make your macro method a public static method. Then create a transformation method that calls that public static method. Then in your macro method, call that public static method. This way you have one place to call the code and it is available in all places (transformations, macros, code behind). Yes it's a bit of extra code, but then your custom method is available everywhere.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brad Vrudney answered on June 4, 2015 19:02

Bob, maybe it would help to see more of your query. It seems as if you are trying to execute your kentico macro inside of sql server, which won't work, the way you have that written. The macro is going to evaluate before it executes the sql, which is why you are seeing it pass the current document into the sql.

Maybe if I understood what you were trying to achieve, I could provide more assistance.

0 votesVote for this answer Mark as a Correct answer

Bo Fu answered on June 4, 2015 19:34

Brad, Thank you for your reply. I just want to run the macro expression (change the format of a string) against the value of a column. Is there any way to do that?

0 votesVote for this answer Mark as a Correct answer

Brad Vrudney answered on June 4, 2015 20:22

You need to perform that action in your transformation. Since you didn't specify, I'm assuming you are using a repeater with custom query, or the like. So in your transformation, you just need to modify the output value, as you are attempting to do. <%MyCustomMacro(Eval("MyField"))%>

0 votesVote for this answer Mark as a Correct answer

Bo Fu answered on June 4, 2015 21:12

I think macro methods can not be called in ASCX transformations. So I have used ajax to solve my problem.

0 votesVote for this answer Mark as a Correct answer

Bo Fu answered on June 5, 2015 15:28

Brenden, that is a great idea, many thanks

0 votesVote for this answer Mark as a Correct answer

Brad Vrudney answered on June 5, 2015 16:19

Sorry Bob, I missed one part to my answer. You can use macros in ascx if you wrap them in a call:

<%# MacroContext.CurrentResolver.ResolveMacros("{#CUSTOMEXPRESSION#}") %>

0 votesVote for this answer Mark as a Correct answer

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