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

Daniel Pasacrita June 24, 2014

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

1 vote
Answer accepted
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 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>

1 vote
Eirik Midttun June 24, 2014

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
TAGS
AUG Leaders

Atlassian Community Events