A simpler way to view attachments in Confluence
by David. Average Reading Time: almost 2 minutes.
…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
## Updated: 2011-03-15
## View page attachments in a simple list
## USAGE: {list-attachments}
##applyDecorator("page")
#set ($attachmentsList = $content.getLatestVersionsOfAttachments())
##<div class="listAttachmentsDiv">
#if ($attachmentsList.size() > 0)
<ul>
#foreach( $attachment in $attachmentsList )
#set ($currentAttachmentId = $attachment.id)
#if ($attachment.comment && $attachment.comment.trim().length() > 0)
## Display the comment as the link text instead of the filename
<li><a href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")">$generalUtil.htmlEncode($attachment.comment)</a></li>
#else
## Display the filename as the link text
<li><a href="$generalUtil.htmlEncode("${req.contextPath}${attachment.downloadPathWithoutVersion}")">$generalUtil.htmlEncode($attachment.fileName)</a></li>
#end
#end
</ul>
#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:

Now you’re left with a simple list of links — much purer than a big fat table of metadata.
For extra points (Updated 2011-03-15)
If you’ve got a might great slab of files attached to the Confluence page, why not wrap this usermacro in the azList macro:
{azlist}{list-attachments}{azlist}
The result:


RT @dvdsmpsn: A simpler way to view attachments in #atlassian #confluence http://tr.im/C0s2
RT @ConfluenceGuru: RT @dvdsmpsn: A simpler way to view attachments in #atlassian #confluence http://tr.im/C0s2
[...] This post was mentioned on Twitter by Dietmar Zipfel and David Simpson, Atlassian Confluence. Atlassian Confluence said: RT @dvdsmpsn: A simpler way to view attachments in #atlassian #confluence http://tr.im/C0s2 [...]
RT @dvdsmpsn: A simpler way to view attachments in #atlassian #confluence http://tr.im/C0s2
RT @barconati:RT @dvdsmpsn: A simpler way to view attachments in #atlassian #confluence http://tr.im/C0s2
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.
We’re now running this fine on Confluence 3.2 as is. I’m not sure why you get this error. Sorry.
Hi,
David, can you please give me a direction on how to sort the list as per lastModificationDate
Thanks
Deepak
Deepak:
$content.getLatestVersionsOfAttachments()returns ajava.util.List<Attachment>.You need to sort the list of attachments using
Attachment.getlastModificationDate().It would be nice to add a sort by parameter to this user macro!
Hello,
do you know of a way to list all attachments in a space, on a page?
thanks,
Lawrence
Lawrence: I’d probably suggest writing a plugin to do this, then do something along these lines…
page.getSpaceKey().page.getAttachments()I’ve been looking for something like this for a while. But this does unordered lists only? I need to make it in alphabetic order. Is there a way to achieve this?
This doesn’t sort alphabetically. You could perhaps do some client side processing using javascript/jQuery e.g.
/**
* Quick & dirty alphanumeric sorter
*
*/
(function($) {
$.fn.azSort = function ()
{
$(this).each(function(){
me = this;
// Sort the list items
var listItems = $(me).children('li').get();
listItems.sort(function(x,y) {
var compareX = $(x).text().toUpperCase();
var compareY = $(y).text().toUpperCase();
return (compareX < compareY) ? -1 : (compareX > compareY) ? 1 : 0;
});
// Write the list items back to the DOM
$(me).append(listItems);
});
}
})(jQuery);
AJS.init(function(){
AJS.$('.listAttachmentsDiv ul').azSort();
});
NB This code is untested, but a good starting point.
In confluence 3.4.7 this is returning
Error formatting macro: list-attachments: com.atlassian.core.exception.InfrastructureException: Error occurred rendering template content
Any ideas?
This macro is beautiful.
But when I implement it in a page, there is always this line written before the macro is displayed:
$helper.page.title
and it is ticketed as a page.
What can I do to hide this line?
I’ve updated this macro a little. Please review the change.
Thanks, David
Thanks for this, its working nicely for me.