Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

How to insert link to page in other space in user macro

Hi, I want to create an annotated variant of the Include Page macro in confluence. My version should surround the included page in a panel and add a caption (included from <link>).

I got it working for pages in the same space, but not for pages in a different space, because I do not know how to set the <ri:page ri:space-key> parameter from my ## @param ip:title=Included Page|type=confluence-content|required=true|desc=Select a page to include parameter:


Example storage format for an include page macro:

<p><ac:structured-macro ac:name="include" ac:schema-version="1" ac:macro-id="7c3338cb-d85f-4aeb-8c61-5fb36e24d61f"><ac:parameter ac:name=""><ac:link><ri:page ri:space-key="TFML" ri:content-title="Instructions for use" /></ac:link></ac:parameter></ac:structured-macro></p>


My user macro:

## @param ip:title=Included Page|type=confluence-content|required=true|desc=Select a page to include
## @param Title:title=Title|type=string|desc=Title

<ac:structured-macro ac:name="panel">
<ac:parameter ac:name="borderColor">#6699CC</ac:parameter>
<ac:parameter ac:name="borderWidth">1</ac:parameter>
<ac:parameter ac:name="title">$!paramTitle</ac:parameter>
<p><ac:structured-macro ac:name="include" ><ac:parameter ac:name="">
<ac:link><ri:page ri:content-title="$paramip" /></ac:link></ac:parameter>
<div><sub><em>(included from <ac:link><ri:page ri:content-title="$paramip" /></ac:link>)</em></sub></div>


Of course, I could add a second @param where the user has to enter the space name, but that would be independend of the page parameter and allow invalid input combinations. 

How can I extract the space from the confluence-content parameter or how can I construct correct links only using the confluence-content parameter?


(if I use a same-space-page in my macro, it works as expected, but for a foreign-space-page, this error message is rendered:


2 answers

1 accepted

0 votes
Answer accepted

I literally spent Tuesday creating this same macro. Today I found your post! (It would have saved me all those hours, but to be honest I think I learned a lot more doing it myself.)

Does this help?


## Macro title: Page Link with Ref
## Macro has a body: No
## Body processing: No Macro Body
## @param PageToInclude:title=Page to Include|type=confluence-content|required=true|desc=To specify a page in a different space, use SPACEKEY:Page Title.

#if ( $paramPageToInclude.indexOf(":") == -1 )
  #set ( $spaceKey = $space.key )
  #set ( $contentTitle = $paramPageToInclude)
  #foreach ( $part in $paramPageToInclude.split(":") )
    #set ( $spaceKey = $part )
  #foreach ( $part in $paramPageToInclude.split(":") )
    #set ( $contentTitle = $part )
#set ( $linkSpace = $spaceManager.getSpace($spaceKey) )

#set ( $spacetitle = $linkSpace.getName())
#if ($spacetitle.contains('Space'))  
   #set ( $spacetitle = "$spacetitle Space")

This is a link to
    <ri:page ri:content-title="$!contentTitle" ri:space-key="$spaceKey"/> $!paramPageToInclude
 in the <a href="$linkSpace.homePage.urlPath"> $spacetitle. </a>

<ac:macro ac:name="panel">
        <ac:parameter ac:name="titleBGColor">lightyellow</ac:parameter>
        <ac:parameter ac:name="borderStyle">solid</ac:parameter>
        <ac:parameter ac:name="borderColor">#6699CC</ac:parameter>
        <ac:parameter ac:name="borderWidth">1</ac:parameter>
        <ac:parameter ac:name="titleColor">#000000</ac:parameter>
  ##     <ac:parameter ac:name="title">This is a link to "$!contentTitle" in the $spacetitle.</ac:parameter>

              <ac:structured-macro ac:name="include">
                <ac:parameter ac:name="">
                    <ri:page ri:content-title="$contentTitle" ri:space-key="$spaceKey"/>


Thank you very much, this helps a lot!

Very nice solution, supports auto-complete in the parameter editor including spaces.

Radek Janata Community Leader Jun 11, 2019

That unclear part using `foreach` can be also rewritten this way:

#set ($delPos = $paramPageToInclude.indexOf(":"))
#if ($delPos == -1)
#set ($spaceKey = $space.key)
#set ($contentTitle = $paramPageToInclude)
#set ($spaceKey = $paramPageToInclude.substring(0, $delPos))
#set ($delPos = $delPos + 1) ## shifting delimiter position by 1
#set ($lenPage = $paramPageToInclude.length())
#set ($contentTitle = $paramPageToInclude.substring($delPos, $lenPage))

Appreciate both of these posts from @Shawn Wilson and @Radek Janata. Amazing the hoops needed to pull out those array elements. I was hoping to use either of these, but no good:

#set ($spaceKey = $paramPageToInclude.split(":")[0])

#set ($pageArray = $paramPageToInclude.split(":"))
#set ($spaceKey = $pageArray[0])

I'm fairly new to custom macro writing, documentation in general seems to be light. Thanks for your posts, they helped me get past a block today.

0 votes
Bill Bailey Community Leader May 02, 2017

What exactly do the $paramip look like when you select a "foriegn page"? You may want to add a debug help in the macro to just display the contents of that variable to see what it is returning. Maybe the space key infor is prepended?

I haven't done this type of page navigation before. As your macro is now, it lets a user search and find a page in any space?

I've tried that. $paramip contains only the page name, no space key. The insert-macro-dialog  allows an auto-complete search over the complete confluence wiki, same as for the original include page macro (by defining type=confluence-content). 

Bill Bailey Community Leader May 09, 2017

Then you are forced to have the user select the space key so that you have that info. Not sure how you trap invalid combinations.

There maybe a way to extract the key data from the page id with a java method. Maybe this will give you enough hints:


These solutions take you to the homepage instead of the direct page. Is there a way to go directly to the page instead of the homepage?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,368 views 7 28
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