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
Accepted answer

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.

0 votes
Bill Bailey Community Champion 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 Champion 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:


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Feb 06, 2019 in Confluence

Try out the new editing experience

Hi team, I’m Avinoam, a product manager on Confluence Cloud, and today I’m really excited to let the Community know that all customers can now try out the new editing experience and see some of the ...

1,021 views 51 8
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