Is there any way for the attachments macro to pull attachments from more than one page?

Basically just the title. I'd like to make the attachments macro pull attachments from two different pages as the same list of attachments. I tried commas and semicolons to list two different pages, but it wouldn't work. Any way to do this?

2 answers

1 accepted

Accepted Answer
1 vote
Davin Studer Community Champion Jun 24, 2014

You could create your own with a user macro. Here is one that I created that mimics the page attachments maco somewhat. I did this because the bundlesd one had (not sure if it still does) massive performance issues with pages that have lots of attachments. It would try and render a thumbnail of each attachment and if the page has hundreds it would bring the server to its knees. So, I created this one that just displays a list of them in a table. You could edit it to work with multiple pages.

## Macro title: tvc_attachments
## Macro has a body: N
## Developed by: Davin Studer
## Date created: 08/07/2013
## This macro will allow you to display a list of the page attachments without the preview functionality included in the built in attachments macro.
## @param Label:title=Attachment Label|type=string|required=false|desc=Enter a label to filter the attachments by.
## @param Page:title=Page Title|type=confluence-content|required=false|desc=If not specified, the current page is used.

#if( $paramPage && $paramPage != "" )
    ##########################
    ## Get the page manager ##
    ##########################
    #set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
    #set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
    #set( $containerManager=$getInstanceMethod.invoke(null,null) )
    #set( $containerContext=$containerManager.containerContext )
    #set( $pageManager=$containerContext.getComponent('pageManager') )
    
    ##########################
    ## Find the page        ##
    ##########################
    #set( $parts = $paramPage.split(":") )
    #set( $i = 0 )
    #set ( $len = 0 )
    #set( $key = "" )
    #set( $name = "" )

    ##Having trouble finding out the length/size of $parts ... brute force it
    #foreach ( $part in $parts )
        #set ( $len = $len + 1 )
    #end
    
    #if ( $len == 1 )
        #set ( $key = $content.spaceKey )
        #set ( $name = $paramPage )
    #else
        #foreach ( $part in $parts )
            #if ( $i == 0 )
              #set ( $key = $part )
              #set ( $i = $i + 1 )
           #else
              #set ( $name = $part )
           #end
        #end
    #end

    #set ( $tempcontent = "" )
    #if ( $pageManager )
        #set ( $tempcontent = $pageManager.getPage($key, $name) )
    #end
    #if ( $tempcontent && $tempcontent != "" )
        #set ( $content = $tempcontent )
    #else
        The page "$paramPage" was not found in this space.
        #set ( $content = "" )
    #end
#end

#set ( $attachments = $content.getLatestVersionsOfAttachments() )

<table class="tvc_attachments">
    <tr>
        <th class="tvc_attachments_filename_column">File</th>
        <th class="tvc_attachments_modified_column">Modified</th>
    </tr>
    #foreach( $attachment in $attachments )
        #if( $paramLabel && $paramLabel != "" )
            #foreach( $lab in $attachment.getLabels())
                #if( $lab == $paramLabel )
    <tr>
        <td class="tvc_attachments_filename_column">
            <a class="tvc_attachments_$attachment.getFileExtension()" href="$attachment.getDownloadPath()" target="_blank">$attachment.getTitle()</a>
        </td>
        <td class="tvc_attachments_modified_column">
            $action.dateFormatter.format($attachment.getLastModificationDate()) by  #usernameLink($attachment.getLastModifierName())
        </td>
    </tr>
                #end
            #end
        #else
    <tr>
        <td class="tvc_attachments_filename_column">
            <a class="tvc_attachments_$attachment.getFileExtension()" href="$attachment.getDownloadPath()" target="_blank">$attachment.getTitle()</a>
        </td>
        <td class="tvc_attachments_modified_column">
            $action.dateFormatter.format($attachment.getLastModificationDate()) by #usernameLink($attachment.getLastModifierName())
        </td>
    </tr>
        #end
    #end
</table>

For the Attachments macro the documentation says: "Displays a list of attachments on a given page." There is another macro called Space Attachements which gives the attachement of the whole space, but that may give you too much. You can filter on file extension though.

Suggest an answer

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

Introducing Praecipio Consulting, an Atlassian Solution Partner

Hey there Community!  My name is Vannya Vallejo, the Channel Communication Specialist at Atlassian and I want to help Atlassian users like you learn about our Solution Partners and how they c...

322 views 0 9
Read article

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