How to insert link to page in other space in user macro
Asked by Jochen Neuhaus:

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:++

<br />

## @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\>  
\<ac:rich-text-body\>  
\<p\>\<ac:structured-macro ac:name="include" \>\<ac:parameter ac:name=""\>  
\<ac:link\>**\<ri:page ri:content-title="$paramip" /\>** \</ac:link\>\</ac:parameter\>  
\</ac:structured-macro\>\</p\>  
\<div\>\<sub\>\<em\>(included from \<ac:link\>**\<ri:page ri:content-title="$paramip" /\>** \</ac:link\>)\</em\>\</sub\>\</div\>  
\</ac:rich-text-body\>  
\</ac:structured-macro\>

-------------

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:

![image.png](/t5/image/serverpage/image-id/831i14035F0D62CA8E2A/image-size/large?v=v2&px=999 "image.png")


Answers:
author: Shawn Wilson
body: 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)
#else
? #foreach ( $part in $paramPageToInclude.split(":") )
??? #set ( $spaceKey = $part )
??? #break
? #end
? #foreach ( $part in $paramPageToInclude.split(":") )
??? #set ( $contentTitle = $part )
? #end
#end
#set ( $linkSpace = $spaceManager.getSpace($spaceKey) )

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

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

<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:rich-text-body>
????????????? <ac:structured-macro ac:name="include">
??????????????? <ac:parameter ac:name="">
????????????????? <ac:link>
??????????????????? <ri:page ri:content-title="$contentTitle" ri:space-key="$spaceKey"/>
????????????????? </ac:link>
??????????????? </ac:parameter>
????????????? </ac:structured-macro>

</ac:rich-text-body>
</ac:macro>
```

comments: author: Jochen Neuhaus
body: Thank you very much, this helps a lot!

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


author: Radek Janata
body: 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)
#else
  #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))
#end
```


author: Danny Aponte
body: Appreciate both of these posts from [@Shawn Wilson](/t5/user/viewprofilepage/user-id/1132913) and [@Radek Janata](/t5/user/viewprofilepage/user-id/716835). 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.


author: Bill Bailey
body: 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?

comments: author: Jochen Neuhaus
body: 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*).


author: Bill Bailey
body: 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:

<https://gist.github.com/chids/870528>


author: Kristen Grimes
body: 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?


Tags: user-macro