Scriptrunner for Confluence - Subpages
Asked by Dieter Schade:

Hello all,

I am quite excited about the Scriptrunner for Confluence. Does anyone of you know if it is also possible to insert a macro on a parent page which displays all the content of the subpages?

Thanks in advance!


Answers:
author: Mirek
body: You can always try to write your own user macro - <https://confluence.atlassian.com/doc/writing-user-macros-4485.html>. It mostly depends how you want to show the content but it should be possible and quite easy to do if you will use in your macro build in include macros or any other useful like expand etc.


author: JamieA
body: Hi - good question. A script macro for this is pretty straightforward. The config might look like:

image2016-5-31 14:58:12.png

No parameters are necessary.

The code is:

```
import com.atlassian.confluence.pages.Pageimport com.atlassian.confluence.xhtml.api.MacroDefinitionimport com.atlassian.confluence.xhtml.api.XhtmlContentimport com.atlassian.renderer.v2.RenderUtilsimport com.atlassian.sal.api.component.ComponentLocatordef xhtmlContent = ComponentLocator.getComponent(XhtmlContent)def entity = context.getEntity()if (entity instanceof Page) {    def concat = (entity as Page).children.collect { child -&gt;        "&lt;h2&gt;${child.title}&lt;/h2&gt;" +            xhtmlContent.convertMacroDefinitionToStorage(new MacroDefinition("include", null, null, [                "0"       : "${context.spaceKey}:${child.title}".toString(),            ]), context)    }.join("\n\n")    return xhtmlContent.convertStorageToView(concat, context)}else {    RenderUtils.blockError("Error", "Should only be used on pages, not comments, blog posts, etc")}
```

Seems to work for me. The secret is to write out a bunch of *include* macros, one for each child page. It also adds the page title as an h2.

This avoids having to deal with permissions errors, relative links and attachments, and so on.

It is kind of easy to do it with user macros, but it's also easy to cock stuff up. For instance, if you don't handle permissions properly, a user who does not have view permissions to some child page could access the content by adding a bad implementation of "include-children" to the root page.

[](https://community.atlassian.com/t5/Confluence-questions/Scriptrunner-for-Confluence-Subpages/qaq-p/206635?attachment-id=11881){#link_1186d4eb111828}  
[](/tnckb94959/attachments/tnckb94959/confluence-questions/34855/1/image2016-5-31%2014%3A58%3A12.png){#display_1186d4eb111828_0}


author: Mark McCormack (Adaptavist)
body: Hi Dieter,

Yes, it is possible to include specific versions of pages using one of the bundled ScriptRunner for Confluence Bundled Macros called [Include Version](https://scriptrunner.adaptavist.com/latest/confluence/macros/BundledMacros.html#_include_version_macro). You could certainly start with that.

If you have many instances of the "include page" macro you can also convert you pages to use the new macro.

We've also bundled an [Includes Report](https://scriptrunner.adaptavist.com/latest/confluence/macros/BundledMacros.html#_includes_report_macro) macro so you can see what versions are included in your parent page and update them all if you wish.

regards,

Mark.


Tags: confluence-administration

scriptrunner