Text/XML transformation: unable to resolve to a property (bool/check-box) of a webpart

Sergiu Filip asked on May 10, 2016 18:46

The property of the webpart is called "HorizontalLayout". I know the following syntax is- from a K# processing perspective - OK; what it doesn't work is resolving to the property of the webpart; that evaluation is always false; pulling my hair here already ;)

{%
if(Eval(HorizontalLayout))  {
    "<ul id='main-menu' class='sm sm-blue sm-vertical collapsed'>"
}
else {
    "<ul id='main-menu' class='sm sm-blue collapsed'>"
}
#%}

Correct Answer

Brenden Kehren answered on May 10, 2016 20:04

I think you have a few things confused here:

A transformation is a record from a page type, which is repeated based on results found or returned from a data operation (query, web service call, etc.). This is typically a property of a webpart (transformation name, alternating transformation name, selected transformation name, etc.) For instance a list of events, there is typically a header or opening element (<ul>, <table>, etc.) and then the listing tags (<li>, <tr>, etc.). The <li> or <tr> elements would be part of the transformation.

In your webpart, the properties you specify are typically set in the code behind of the webpart. The properties will not be part of a transformation ever unless you were to code this within the webpart itself.

To answer your question, the best approach is to set this property would be to create a new webpart layout and specify the logic in your post as needed (minus the Eval() because you're accessing a public property of the webpart). If your property HorizontalLayout was a field in a page type, then you can use it in your transformation, but since it is not, your best bet as I mentioned, is to use a new webpart layout.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Anton Grekhovodov answered on May 10, 2016 18:50

Hi,

In text/xml transformation you can't use Eval method, just remove it

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on May 10, 2016 19:06 (last edited on May 10, 2016 19:38)

See this for more details on macro syntax.

In your case I'd write something like this:

<ul id='main-menu' class='sm sm-blue collapsed {% if ( HorizontalLayout == "True") return "sm-vertical";%}'>

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on May 10, 2016 19:40

Thank you Anton: well, I tried it both ways before I posted this. Thank you Roman: I did consult the documentation before posting (and pulling my hair for couple of hours); is there anything in particular you can point me to, please ? (otherwise - that topic is a pretty broad one).

Again, the question is: how do I get the value of a web-part property, in a text/xml transformation.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on May 10, 2016 19:49 (last edited on May 10, 2016 19:50)

Oh, I see.. I misunderstood your question.

What about changing your transformation to ASPX and using server side code? See how you could access web part properties here.

However changing transformation code based on web part property is, in my opinion, similar to changing transformation property of the web part, so why don't you want to implement two transformation and select one you need?!

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on May 10, 2016 21:43

Thank you Brenden, your clarification helped: I was indeed severely confused. I will look into the web-part layouts.

1 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on May 11, 2016 19:29

Hi Sergiu,

If you are not hell bent on using Text/XML can use ASCX transformations then their is an easy way to do this. I think ASCX have more methods available.

Using If - If(Object value, trueValue, falseValue) <%# If(Eval("HorizontalLayout"), "<ul id='main-menu' class='sm sm-blue sm-vertical collapsed'>", "<ul id='main-menu' class='sm sm-blue collapsed'>")%>

Also there are methods like:-

  1. IfCompare
  2. IfTrue

that you can use also as per your requirements.

Refernce Methods

1 votesVote for this answer Mark as a Correct answer

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