Adding Page-Type page as a field type for another Page-type

Hosam Abdeltawab asked on August 20, 2020 21:26

I am using Kentico 12 MVC. Currently, I am trying to create to page types, one is called Parent page type, the other is called Children page type. Now, in the Parent page type I want to have a field that is a list of the Child page type. For example, if I have multiple Child pages types that contain links, the Parent page type field would contain all those links. How would I might be able to do that?

Correct Answer

Liam Goldfinch answered on August 20, 2020 23:16

Hi Hosam,

I think the best way to achieve what you've described is through adding a Long Text field (using the form control Uni Selector) to the Parent page type, and configure it like this:

  • Object type: CMS.Document
  • Return column name: NodeGuid
  • Display name format: {%DocumentName%}
  • Selection mode: Multiple
  • Where condition: ClassName='Example.Children'

Swap Example.Children with the ClassName of your Children page type.

The Parent page you've created in the tree should now be able to select multiple Children page types and store the associated NodeGuids against the Parent.

Then in the Controller when you are retrieving the data for the Parent page type, you can also lookup the relevant data from the associated Child pages, see this gist for some example code: https://gist.github.com/liamgold/98063c38734e5008e7bf0edb071c3ba2

1 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on August 21, 2020 10:59

Hi Hosam,

Generally, there are 2 approaches you can use for it:

  1. Utilize Pages control (explained in this documentation article), it allows you to select and order multiple pages from the tree and there is support for it in code generation as well. If you have DancingGoat sample website, check out how Related Articles are implemented in Article page type.
  2. Always create child pages under a parent page, in this case in parent page you will have .Children property so that you can always access child pages and use their links. The only downside of this approach is that in Kentico 12 MVC you cannot create (link or reuse) the same child item for multiple parent pages. However, this will be possible to do in the next Kentico version by utilizing linked pages. You can check this in Kentico 13 Beta version. Going forward this will be a recommended approach.

Solution provided by Liam will also work, but it has a challenge: node guids will be stored in a text field, so that you would need to parse these into list of guids and fetch the content by these guids. When Pages control will do it for you out-of-the-box. Also, Kentico Pages control stores these references as Relationships, meaning if the referenced page will be deleted - the Relationship will be deleted as well. If it is stored as a text field you will need to do it manually or write some custom code to clean this up.

1 votesVote for this answer Mark as a Correct answer

Hosam Abdeltawab answered on August 21, 2020 16:43

I am receiving the following error when I try to add the links into the parent page type. [DataConnection.HandleError]: Query: WITH AllData AS ( SELECT [DocumentName], [NodeGuid], ROW_NUMBER() OVER (ORDER BY [DocumentName]) AS [CMS_RN] FROM View_CMS_Tree_Joined AS V WITH (NOLOCK, NOEXPAND) LEFT OUTER JOIN COM_SKU AS S WITH (NOLOCK) ON [V].[NodeSKUID] = [S].[SKUID] WHERE (link.GwicLink) ) SELECT *, (SELECT COUNT(*) FROM AllData) AS [CMS_TOT] FROM AllData WHERE CMS_RN BETWEEN 1 AND 10 ORDER BY CMS_RN Caused exception: An expression of non-boolean type specified in a context where a condition is expected, near ')'.

The configuration for the parent page is:

Form Control: Uni selector
Object type: CMS.Document
Return column name: NodeGuid
Selection Mode: Multiple
Where Condition: link.GwicLink //This is the code name for my child's page type
0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on August 21, 2020 16:52

Simply amend your where condition to be like this:

Where condition: ClassName = "link.GwicLink"

As you can see from your error message, whatever you put here will be directly added to your WHERE condition in SQL. So you can try it first in SQL management studio before using.

0 votesVote for this answer Mark as a Correct answer

Hosam Abdeltawab answered on August 21, 2020 17:06 (last edited on August 21, 2020 17:07)

I am getting an invalid column name for "link.GwicLink"?

[DataConnection.HandleError]: Query: WITH AllData AS ( SELECT [DocumentName], [NodeGuid], ROW_NUMBER() OVER (ORDER BY [DocumentName]) AS [CMS_RN] FROM View_CMS_Tree_Joined AS V WITH (NOLOCK, NOEXPAND) LEFT OUTER JOIN COM_SKU AS S WITH (NOLOCK) ON [V].[NodeSKUID] = [S].[SKUID] WHERE (ClassName = "link.GwicLink") ) SELECT *, (SELECT COUNT(*) FROM AllData) AS [CMS_TOT] FROM AllData WHERE CMS_RN BETWEEN 1 AND 10 ORDER BY CMS_RN Caused exception: Invalid column name 'link.GwicLink'. In my child page type general properties, the code name is "link.GwicLink" where "link" is the name space and "GwicLink" is the code name. Should I use the table name instead?

0 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on August 21, 2020 17:23

Hi Hosam,

You need to use single quotes, not double. Use the where condition ClassName='link.GwicLink'

0 votesVote for this answer Mark as a Correct answer

Hosam Abdeltawab answered on August 21, 2020 18:52

In added gist, line 18 contains childrenNodeGuids, how is that defined? Also, i checked the DB for the parent page type after I selected the children page types as children and the DB contains only one row for the parentID and null for the children column:

LinksContainerID Links
1 NULL

Am I missing something?

0 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on August 22, 2020 23:22 (last edited on August 22, 2020 23:24)

Hi Hosam,

ChildrenNodeGuids on line 18 is the field name I've added to my example Parent page type. This should contain a semi-colon separated list of NodeGuids of all the Child page type pages you've selected.

If I run the following SQL statement:

SELECT [ParentID]
  ,[ParentName]
  ,[ChildrenNodeGuids]
FROM [Kentico12].[dbo].[Example_Parent]

I get the following returned:

ParentID ParentName ChildrenNodeGuids
1 Parent 20444620-3f92-4f43-bc47-9042ad761d9e;c1b99d19-4f73-4279-b3d9-27060c72bb13

These nodeguids are then used for the lookup in the 2nd query on line 22.

As for why your database query returns NULL for the Links field, are you sure you've selected the child pages and then saved the parent document? If you reload the parent document in the tree, is the UniSelector field empty again?

Also can you double check you've configured the UniSelector control correctly for your field? Double check the settings provided in my original solution.

0 votesVote for this answer Mark as a Correct answer

Hosam Abdeltawab answered on August 24, 2020 16:04

How might I separate the links in each page type? For instance, I have two page types Parent page-type and Child page-type. I created two parent page-types and underneath each parent is a child.

When I try to select pages form the parent page-type, I get the option to select any child from either parent. I want to be able to select only the children inside the current parent not any child for any Parent?

So if I have two parents and both of them have children, it doesn't matter which parent I am on, I am able to select children from both parents, and I want to only select children from the current parent.

0 votesVote for this answer Mark as a Correct answer

Liam Goldfinch answered on August 24, 2020 20:17

Hi Hosam,

To do that you could amend the where condition to be:

ClassName='link.GwicLink' AND NodeAliasPath like '{% EditedObject.NodeAliasPath #%}%'
0 votesVote for this answer Mark as a Correct answer

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