Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can I get all attachments for a space?

Brett Ryan
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.
July 9, 2012

How can I retrieve all attachments for a space?

I want to do something similar to the following though for an individual space:

Iterator<Attachment> i =
    attachmentManager.getAttachmentDao().
    findLatestVersionsIterator();
    while (i.hasNext()) {
        Attachment a = i.next();
        // Process attachment.
    }
}

Works a treat, though instead of going through every attachment in the system I only want to go over certain spaces but get the current version only.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 16, 2012

There's no methods on the AttachmentManager or the AttachmentDAO to get all the attachments in a particular space.

You would need to use the SpaceManager to get all the pages in the space, and then iterate through them one-by-one and use the AttachmentManager to get all the attachments on each page.

Alternatively, you could use the Lucene index to retrieve this information. This will perform much better, but the downside is it may be up to a minute or so out-of-date. Have a look at how the {space-attachments} macro built-in to Confluence does it - https://studio.plugins.atlassian.com/source/changelog/CONFATT?cs=167076

Brett Ryan
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.
August 1, 2012

While this would work for me it turned out to be slow. Instead I identified all space keys I wanted to process, then use the iterator pattern in my original post and check the attachments space key:

Set<String> keys = getSpaceKeys();
Iterator<Attachment> i =
        attachmentManager.getAttachmentDao().
        findLatestVersionsIterator();
while (i.hasNext()) {
    Attachment a = i.next();
    if (a.getSpace() != null &&
            keys.contains(a.getSpace().getKey())) {
        // Process attachment
    }
}

This turns out quite effective.

TAGS
AUG Leaders

Atlassian Community Events