…or how to display attachments in an unordered list.
Something that bugs me in Confluence is the way that attachments are shown. The default view is a big table with just about all the information related to each attachment.
This is great if you want all this detail, but I’m a simple soul with simple needs.
Krug suggested “Omit needless words”. In this case, it’s more of “Omit needless metadata”.
Less is more
Enter {list-attachments} – a user macro that dispenses with the excess. It displays an unordered list with the filename, file size and last modified date. If there’s a comment for the attachment, this is displayed in the link text rather than the filename.
This is a really straightforward view of the attachments on the page.
How do I install {list-attachments} ?
It’s pretty easy. As a Confluence administrator, go to Confluence Admin | User Macros and click on Create a User Macro.
Macro name: list-attachments
Template:
## MACRO: list-attachments
## View page attachments in a simple list
## USAGE: {list-attachments}
#set ($attachmentsList = $content.getLatestVersionsOfAttachments())
#applyDecorator("page")
<div class="listAttachmentsDiv">
#if ($attachmentsList.size() > 0)
<ul>
#foreach( $attachment in $attachmentsList )
<li>
## Display the file type icon:
<a name="$generalUtil.htmlEncode($generalUtil.urlEncode($page.title))-attachment-$generalUtil.htmlEncode($generalUtil.urlEncode($attachment.fileName))">#parse ("/pages/includes/attachment_icon.vm")</a>
## The link:
#if ($attachment.comment && $attachment.comment.trim().length() > 0)
## Display the comment as the link text instead of the filename
<a href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")">$generalUtil.htmlEncode($attachment.comment)</a>
#else
## Display the filename as the link text
<a href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")">$generalUtil.htmlEncode($attachment.fileName)</a>
#end
## Display the file size:
<span class="fileSize">$attachment.niceFileSize</span>
## Display the last modification date
<span class="lastModDate">$attachment.lastModificationDate</span>
</li>
#end
</ul>
#else
## Display nothing
#end
</div>
#end
Accept the defaults for the other fields, then click on Save
To call the macro in the page type: {list-attachments}
The result:

For extra points
If you want to change the way the items are displayed, add some CSS to the global stylesheet, e.g
.listAttachmentsDiv a { font-family: helvetica, sans-serif; }
.fileSize, .lastModDate { font-size: 90%; color: #999;}
To see even less, comment out (add ## at the start of the line) lines 27 & 30 in the user macro to remove the file size and last modification date.
Now you’re left with a simple list of links.
Comments
4 Comments so far. Leave a comment below.Nice work, David. This could be a nice enhancement when you don’t want that huge block stretching across your full page.
My own worry about user macros is keeping them up to date with any other changes to the base confluence functionality. This is especially the case if you’re wrapping other macros. I ran into this problem with my Related Content User Macro (http://confluence.atlassian.com/x/LCOyCg).
Do you have any policies you use when building user macros?
Brad: I think this is the same problem you’d have with for example updating themes when the base functionality changes.
Luckily, this user macro is very easy to update if the base functionality changes. It is based on altering a stripping most of the functionality from
<confluence-install-dir>/confluence/pages/viewattachments.vmHi – I get the following error:
Error formatting macro: list-attachments: com.atlassian.core.exception.InfrastructureException: Error occurred rendering template content
Running Confluence 3.1.1. Any ideas?
Thanks
Mike
Mike: I’ve not had a chance to check and update this for Confluence 3.1 as yet — we’re still running 3.0.2. I’ll post an update and drop you a line when I do.
Trackbacks
5 pings so far.