Scriptrunner for Confluence - Subpages

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

This widget could not be displayed.
Mirek Community Champion 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.

This widget could not be displayed.

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.

 

 

This widget could not be displayed.

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
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

237 views 2 4
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you