# getParentReference does not point to logical Parent

**URL:** <https://forum.xwiki.org/t/getparentreference-does-not-point-to-logical-parent/6016>\
**Category:** Help / Discuss\
**Created:** [December 20, 2019, 1:48pm UTC](https://forum.xwiki.org/t/getparentreference-does-not-point-to-logical-parent/6016 "2019-12-20T13:48:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![inhji](https://forum.xwiki.org/user_avatar/forum.xwiki.org/inhji/32/561_2.png) [@inhji](https://forum.xwiki.org/u/inhji)\
**Post date:** [December 20, 2019, 1:48pm UTC](https://forum.xwiki.org/t/getparentreference-does-not-point-to-logical-parent/6016/1 "2019-12-20T13:48:19Z")

</div>

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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [December 20, 2019, 2:12pm UTC](https://forum.xwiki.org/t/getparentreference-does-not-point-to-logical-parent/6016/2 "2019-12-20T14:12:26Z")

</div>

See [https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/ContentOrganization/NestedPagesMigration/](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:

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

```

Hope this helps,  
Marius

---

<div class="post-metadata">

**Author:** ![inhji](https://forum.xwiki.org/user_avatar/forum.xwiki.org/inhji/32/561_2.png) [@inhji](https://forum.xwiki.org/u/inhji)\
**Post date:** [December 23, 2019, 9:43am UTC](https://forum.xwiki.org/t/getparentreference-does-not-point-to-logical-parent/6016/4 "2019-12-23T09:43:24Z")

</div>

Thanks Marius,

this works perfectly!
