Get Parent Node and check custom field

Alonso Gonzalez asked on October 24, 2017 16:59

Hi there ,

I'm new to this kentico world, and I have a couple of questions for a bug Im working on. I have a tree structure like :

  • Root ( master page )
  • > Level 1
  • >> Child Level 1 - A
  • >> Child Level 1 - B

Basically I can have any number pages inside and I want to be able to add a custom field to my page type "IsPageX", and want this field to only show at Level 1 childs of the root. What's the right way to include this into the visibility condition for this field to only show at level 1?

After setting this field to only show at level 1 , I need to query it's value from two places,in my master page to include a class into the body tag and be able to add css to modify the PageX styles, and also I need it from a custom web part to conditionally load or not an image, for what I need to obtain the current page node level 1 parent in which Im loading the web part into?

Any help is appreciated,

Correct Answer

Brenden Kehren answered on October 24, 2017 21:32

So assuming you have this new field IsPageX added to all page types, you will have to set the visibility on the field itself on each page type this field is on. So on your IsPageX field, scroll to the bottom of the field definition and find the visibility condition property and click "Edit" button. Click the Code tab at the top and click OK to the windows alert and enter the macro to only show the field at level 1 EditedObject.NodeLevel == 1. This should resolve the first issue you're having troubles with.

For the second issue, getting that value in the master page as well as a custom webpart. You can use the code:

bool isPagex = ValidationHelper.GetBoolean(CurrentDocument.GetValue("IsPageX"), false);

If you place this code in the master page layout it will set the css class for you based on he IsPageX field:

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
      if (CurrentDocument != null)
        bool isPagex = ValidationHelper.GetBoolean(CurrentDocument.GetValue("IsPageX"), false);
        CMS.DocumentEngine.DocumentContext.CurrentBodyClass += (isPagex)? " class-1" : " class-2";
2 votesVote for this answer Unmark Correct answer

Recent Answers

Peter Mogilnitski answered on October 24, 2017 21:40 (last edited on December 10, 2019 02:31)

Normally you don't do isPageX. IsPageX is your page type.

> Level 1 (Electronics)
>> Child Level 1 - A (Cellphones)
>> Child Level 1 - B (Tablets)

So Cellphones and tables are different page types. To check it on master page you do something like if CurrentDocument.ClassName == 'Custom.Cellphones' If you want to go by level you can do something {% CurrentDocument.NodeLevel == 2 |(identity)GlobalAdministrator%}

But if really wanna hide the field based on level in the tree you can, go your page type, choose your field IsPageX and put visibility condition in the advanced settings:

Image Text

0 votesVote for this answer Mark as a Correct answer

Alonso Gonzalez answered on October 24, 2017 23:29 (last edited on October 24, 2017 23:46)

Thank you both for the answer,

One thing is still not clear for me, if I am navigating on page : >> Child Level 1 - B, or any other sublevel of the XPage, how can I query the IsPageX of the NodeLevel == 1 ?

bool isPagex = ValidationHelper.GetBoolean(CurrentDocument.GetValue("IsPageX"), false);

I know I can use NodeParentID but that will only return previous parent and I'm can't know how deep the tree may go.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 25, 2017 06:08

You can use a string function to get the current alias path and then do a split by the forward slash to get the first part of the URL and then do a lookup.

For instance if your current page alias path was /Level-1/Sublevel-2/page-1.aspx,

this CurrentDocument.NodeAliasPath.Split("/")[1] would return: Level-1 so you could do a lookup using the documents API based on that substring as a path to get the single document at the top level you're looking for.

1 votesVote for this answer Mark as a Correct answer

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