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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
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 (Adaptavist)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
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
TAGS
AUG Leaders

Atlassian Community Events