getParentReference does not point to logical Parent

I have a template provider which creates some nested pages like:

  • Page Root
    • Subpage 1
    • Subpage 2

I assumed that Subpage 1 would have a parent reference to Template Root. However, Subpage 1's parent reference is a reference to the Template Root (The Template of Page Root)


This is the code I’m working with:

def docSource = xcontext.method.input.get(1)
def ycontext = xcontext.method.input.get(2)
def document = ycontext.getWiki().getDocument(docSource, ycontext)
  
def parent = xwiki.getDocument(document.getParentReference())

System.out.println(document)
System.out.println(parent)

produces this output:

Entwicklung.Handbuch\.alpha1.Protokolle.2.WebHome // The actual pages
ReadingProtocol.Template.Startseite.WebHome // The template

How can I get from Subpage 1 to Page Root when getParentReference() does not actually point to it?

EDIT:

The code I posted is actually in an EventListener that listens to the DocumentCreatingEvent. Maybe that’s the issue?

See https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/ContentOrganization/NestedPagesMigration/ . document.getParentReference() returns the parent from the deprecated parent-child hierarchy, not from the nested pages hierarchy that is used by default. I don’t think there is currently an API to give you directly the nested page parent directly, but you can compute it relatively easy like this:

def parentReference = document.getDocumentReference().getParent();
if ("WebHome".equals(document.getName())) {
  parentReference = parentReference.getParent();
}
def parent = xwiki.getDocument(parentReference)

Hope this helps,
Marius

Thanks Marius,

this works perfectly!