Macros in Transformations

Sherry Collins asked on June 24, 2015 20:09

Is this statement legal. I need to get the GroupName included in the href.

<a href="/Workgroups/{% CommunityContext.CurrentGroup.GroupName %}/<%#Eval("LinkTo")%>"><%#Eval("MenuItem")%>

should evaluate to /Workgroups/InformationSystems/Projects.aspx

The macro is killing my link.

Recent Answers


Brenden Kehren answered on June 24, 2015 20:18 (last edited on June 25, 2015 01:22)

No. You don't use macros in ascx transformations as a best practice. See macros are extensions of actual code behind. I make it a practice whenever I make a macro to make the method/function being called in the macro publicly available so it can be used in transformations and other places. In the case of Kentico created macro methods, almost every one of them are available by using the full namespaced name.

In your case you can use something like this:

<a href='/Workgroups/<%# CMS.Community.CommunityContext.CurrentGroup.GroupName %>/<%# Eval("LinkTo") %>'><%# Eval("MenuItemName") %></a>
1 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 24, 2015 20:30

I am getting this error

The name 'CommunityContext' does not exist in the current context

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on June 24, 2015 21:58

You need to use the fully qualified name. The easiest way to find out is to type the communitycontext class into a visual studio c# page, and then hover over it and you will get the containing class. You need to have the full class before the CommunityContext class so it can resolve correctly.

It should look something like CMS.Community.CommunityContext.CurrentGroup.GroupName

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 25, 2015 14:07

This is still not working. I am getting an object reference error. The class is fully qualified as I verified in the C# code. Any other ideas of how to get a Group Name.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on June 25, 2015 16:11

Can you paste what you have as far as the code in the transformation so we can see where the issue would be.

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 25, 2015 16:34

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 25, 2015 16:34

<div class="TopMenu">
<a href='/Workgroups/<%# CMS.Community.CommunityContext.CurrentGroup.GroupName %>/<%# Eval("LinkTo") %>'><%# Eval("MenuItemName") %></a>
<br>
</div>
0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on June 25, 2015 17:07

Where is the menuitemname coming from? Can you change that to the group name again and see if that works? Try to place this: <%# CMS.Community.CommunityContext.CurrentGroup.GroupName %> right before the ending a tag, and see if it resolves. The groupname should resolve. If that works, then the issue is in the menuitemname.

0 votesVote for this answer Mark as a Correct answer

Sherry Collins answered on June 25, 2015 18:57

Thanks Josh for all your help. I came at this another way. Instead of in-line sql, I created a stored proc and passed CommunityContext.CurrentGroup.GroupName into the stored proc. Then I could concatenate the path and read it from the Transformation. May not be the most elegant, but I have a demo tomorrow and I had to get it working.

Thanks again.

0 votesVote for this answer Mark as a Correct answer

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