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

Custom macro - Display generated link to attachment of another page only if target exists Edited

Hi everybody

I am stuck at a custom macro I hope someone can help me with.

It takes these parameters:
$parampage = another Confluence page
$paramextension = filetype (e.g. pdf, doc...)
$paramcaption = link text that is shown as a result

The macro does the following:

  1. It takes $parampage and determines $pageid of that page.
  2. Then it creates a link to the path where the attachments of $parampage are stored and in there specifically to this file:
    TITLEOFCURRENTPAGE.$paramextension

The link is created just fine and it also points to the correct file in the correct space and I can for example open the pdf.

The problem is, that I only want to display the link, if the file (e.g. titleofpage.pdf) exists. I tried all kinds of stuff I can't even remember anymore but nothing worked so far.

Here's the code with the commented line "NOT WORKING YET !!!!", where the check that I don't get to work could take place.

## @param page:title=Page|type=confluence-content|required=true|desc=Seite auswählen, die den Anhang beinhaltet
## @param extension:title=Dateierweiterung|type=string|required=true|desc=Dateierweiterung des Anhangs (z.B. pdf oder xlsx)
## @param caption:title=Link Text|type=string|required=true|desc=Text, der für den Link auf der Seite angezeigt werden soll

#############################################################################
## create strings for "space key" and "page name" from parameter page
## source: https://community.atlassian.com/t5/Answers-Developer-Questions/Autocomplete-and-link-to-page-in-user-macro/qaq-p/522841
#############################################################################
#set ( $colonIndex = $parampage.indexOf(":") )
#if ( $colonIndex == -1 )
#set ( $spaceKey = $space.key )
#set ( $pageName = $parampage )
#else
#set ( $spaceKey = $parampage.substring(0, $colonIndex) )
#set ( $pageNameIndex = $colonIndex + 1 )
#set ( $pageName = $parampage.substring($pageNameIndex) )
#end
#set ( $page = $pageManager.getPage($spaceKey, $pageName) )

#############################################################################
## determine page id of that page because attachments are placed in a subfolder named after the id of the page
#############################################################################
#set($pageid=$pageManager.getPage($spaceKey, $pageName).getIdAsString())
#set($linktoatt="https://SOMEWIKIPAGE/download/attachments/$pageid/$content.title.$paramextension")
##$linktoatt #uncomment to see string

#############################################################################
## check if page and attachment exists and create a link to the attachment if both positive
#############################################################################
#if($page) ## only proceed if page exists, works fine
#if(!$linktoatt.getBody()) ## only proceed if attachment exists !!! NOT WORKING YET !!!!
<a href=$linktoatt> $paramcaption </a>
#else
Es existiert auf der Seite <i>$page</i> kein Anhang mit Dateinamen <i>$content.title.$paramextension</i>
#end
#else
Seite $parampage existiert nicht
#end

Do you have any clue?

Thanks a lot,
Mark

0 answers

Suggest an answer

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

How To: get the most out of the new Confluence navigation and Home

Hi Community, Elaine again from Confluence Cloud Product Management. Most of you have used the new Confluence navigation and Home for some time by now. Not sure what changed or just want to lear...

305 views 2 9
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