It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner for Confluence - Subpages

Dieter Schade May 30, 2016

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!

3 answers

1 accepted

0 votes
Answer accepted
Mirek Community Leader May 31, 2016

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.

1 vote
JamieA May 31, 2016

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.Page
import com.atlassian.confluence.xhtml.api.MacroDefinition
import com.atlassian.confluence.xhtml.api.XhtmlContent
import com.atlassian.renderer.v2.RenderUtils
import com.atlassian.sal.api.component.ComponentLocator

def xhtmlContent = ComponentLocator.getComponent(XhtmlContent)

def entity = context.getEntity()
if (entity instanceof Page) {
    def concat = (entity as Page).children.collect { child ->
        "<h2>${child.title}</h2>" +
            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.

 

 

0 votes
Mark McCormack May 31, 2016

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. 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 macro so you can see what versions are included in your parent page and update them all if you wish.

regards,

Mark.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

915 views 0 19
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you