Transformations are very powerful tools within Kentico. Knowing how to take them to the next level requires some knowledge of the transformation as well as the controls you plan to use within the transformation. Today I'll show you how I display related documents within a transformation.
So you asking, why would anyone want to do this? Answer: because the client wants it. If the client wants it, the client gets it.
On one of my latest projects I had a requirement to have people related to locations. Seems simple enough until I learned there was a need to create a search page with the results of the people WITH the related locations within the results.
As I worked through this I knew there had to be a simple way to get these to work in a simple fashion. So why not head over to the
Kentico Documentation to see what I can come up with. If you look at the very bottom of the documentation page, it gives a small code example of how to use a repeater to display the related docs. This is find and dandy if you know what your values are. In my case I didn't know a few of them and in true nature of an efficient (or
lazy) developer I wanted to make it dynamic so I can reuse it later in other places.
So here's what I know:
- Relationship is a one to many
- The person has many locations related to it
- The person is on the left side (important for Kentico terminology)
In my Person page type, I created a simple transoformation just to get a list of them . Next I went to work in Visual Studio to write a little test code to make sure I didn't have any syntax errors. Then I pasted it into my list transformation and tryed it out and by golly, I got it right the first time!
Here's my transformation code:
<script runat="server" type="text/javascript">
protected override void OnInit(EventArgs e)
{
Guid guid = ValidationHelper.GetGuid(Eval("NodeGuid"), Guid.Empty);
dlLocations.RelationshipWithNodeGuid = guid;
dlLocations.ReloadData(true);
base.OnInit(e);
}
</script>
<h2><%# Eval("Name") %>, <%# Eval("Degree") %></h2>
<div class="row">
<cms:CMSRepeater ID="dlLocations" runat="server" Path="/Locations/%" RelationshipName="isrelatedto" ClassNames="custom.location" TransformationName="custom.location.listingdisplay" />
</div>
So now the explanation on what's going on. You can see the generic properties set on the CMSRepeater, the static ones that I know. But for every person that is displayed, I need to get that NodeGUID and apply it to the dlLocations repeater. Easiest way to do it is to is in the OnInit event. It takes the NodeGuid of the person (left side) and sets the dlLocations repeater RelationshipWithNodeGuid property so it will get all the locations (right side) and dispaly them out in the same transformation.
So not a lot of to get this done. There are some folks who will talk about using < script > tags in your transformation vs. creating a custom transformation method. Honestly there isn't much difference BUT if you do plan on reusing this all over in different transformations, then you might look into a custom transformation method as it is just good practice.
Well that's it! Best of luck and Happy Coding!
Posted by:
Brenden Kehren on September 25, 2015
Tags:
how to,
related documents,
transformations,
v8