Using two page types in a repeater

Shawn Ebeyer asked on December 29, 2015 20:25

Hi there,

I have a repeater that is grabbing data from two page types. I am having two issues though.

One, I have a field in both page types named "Display Name" - how do I work with this? If I call the field somewhere, how do I ensure I am being returned the correct data?

The second issue is puzzling me. The repeater is now mashing up the returned data. It seems like its looping over all instances of the second page type I added, which is no good. It's spitting out too much information, where all I need to do is grab some fields from the second page type I added.

How can I make sure this does not happen? Will I have to write a WHERE clause?

Any help is as always, much appreciated.

Thanks,

Shawn

Recent Answers


Bryan Soltis answered on December 29, 2015 23:04

Hi Shawn,

A couple of things:

  1. When you have a repeater, you are actually querying a View behind the scenes that contains all of the fields for the Page Type, as well as the associated Document Fields (NodeID, DocumentName, etc.).

  2. When you specify multiple page types for the repeater, Kentico will do a union on each view for the specified page type. Meaning, if you specify Blog and Phone, it will pull all of the fields for Blogs and Phones into a single recordset.

  3. You should be specify the specific columns you need in your "Select Columns" field on the repeater. By default, the repeater will pull every filed from the view(s), which can drastically increase the returned data and slow down the site. Specify only the columns you need will prevent this and limit the data returned. In the event the Page Types have a common field name (like DocumentName, etc.), the field for each record will be accessible. If one of the PageTypes does not have the filed (Example: Phone does not have the BlogName field), then nothing will be returned for that row.

  4. Repeaters deal with a recordset of data. When you define a transformation, it will loop over each record in the recordset and the transformation displays the value. That is why you are getting "mashed up" records. From Kentico's standpoint, all of the data is one big set.

  5. Your WHERE condition will be applied to the overall recordset, meaning it will filter on both Views (if you have 2 Page Types specified). If you have a WHERE Condition that is specific to a Page Type (filtering on a BlogName or something), it will also filter out all of the "joined" paged types using the same filter.

For these reasons, you should specify multiple Page Types when you know you want all of the data (or at least the filtered data when using a WHERE condition) from both sets.

So what to do? You have a couple of options:

  1. You can use the repeater and specify multiple page types, assuring that there is common data between them (Published Date, Name, etc.). If you apply a WHERE condition, be sure it will still return the data you want fr the types you selected.

  2. You could use multiple repeaters. If you have multiple types of data to display, you may need to use a repeater for each type so you can specify the correct WHERE condition. This may introduce a layout issue, however, as Kentico will process these indivudally.

  3. You can put custom code in your transformation. If you have a repeater with multiple page types and you need to do something special for one of the types when it's displayed, you can look into putting server-side code into your transformation (using a script tag). This would allow you to do something special depending on the type of page type that is being displayed.

  4. You maybe could use a Hierarchical Viewer. This control is used to display different types of data. It's more for structured data (where pages have childrenm, etc) so I'm not sure it is a good fit. You can find more about it here.

  5. You can build a custom control. It may be that you will have to build a custom webpart to do what you want to do. You could have multiple WHERE condition fields that would apply to each page type or something. Then in your custom code, you can specify them separately to get the results you expect.

Hopefully this helps. You have a lot of variables in the scenario so I'm not sure which one would be best.

  • Bryan
0 votesVote for this answer Mark as a Correct answer

Shawn Ebeyer answered on December 29, 2015 23:21

Thank you so much for your detailed answer Bryan,

Definitely keeping all of this in mind moving forward with this project!

You should be specify the specific columns you need in your "Select Columns" field on the repeater. By default, the repeater will pull every filed from the view(s), which can drastically increase the returned data and slow down the site. Specify only the columns you need will prevent this and limit the data returned. In the event the Page Types have a common field name (like DocumentName, etc.), the field for each record will be accessible. If one of the PageTypes does not have the filed (Example: Phone does not have the BlogName field), then nothing will be returned for that row.

Seeing as I have two page types with a common field name, how would I target one of them?

Thanks,

Shawn

0 votesVote for this answer Mark as a Correct answer

Bryan Soltis answered on December 29, 2015 23:49 (last edited on December 29, 2015 23:50)

Well, you don't necessarily "target" them. Because the returned recordset will be both of the sets combined, it will loop through each row for the transformation. Because of that, you can just specify "DocumentName" and it will apply to both. Because each type of page has that field in it, you'll get a value just by using the field name.

If you are referring to specifying the field in the WHERE condition, it doesn't work because of the implicit and explicit joins that are happening behind the scenes in Version 9.

It is worth noting that in Kentico 9, the page-specific views were removed. https://docs.kentico.com/display/K9/Release+notes+-+Kentico+9

If you need to do a column-specifc WEHRE, it will not work because Kentico will apply the WHERE condition to all of the page types behind the scenes.

You could, however, target them in your transformation. You can select "ClassName" as one of the Selected Columns and do a if Eval("ClassName") == "CMS.Blog" type of statement to conditionally show / hide some values or something.

0 votesVote for this answer Mark as a Correct answer

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