How to get all attachments under a space via REST API

Joel Martinez December 10, 2012

I'm currently using the Confluence REST API to develop a custom application outside of our Confluence installation.

I'd like to be able to list ALL attachments associated with a space. This includes attachments of rootpages, children, children's children, children's children's children, and so on... Currently, it doesn't seem there is an easy way to do this. I know I can use the expand tag to manually specify how deep I'd like to go to unveil attachments. For example, to get the rootpages attachments and their immediate children's and I can do something like this:

/space/{key}?expand=rootpages.content.attachments,rootpages.content.children.content.attachments

This seems awfully verbose, to begin with. Also, I'm only getting the first level of children's information.

It appears as if this functionality is available in the full version of the product via this URL: /confluence/spaces/listattachmentsforspace.action?key={key}.

Any ideas?

Thanks!

3 answers

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
Adam Laskowski
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.
December 10, 2012

The API doesn't have a single method to return all attachments in a space; however, I recently created a Python script that fetches all pages in a space and iterates through them to perform various testing tasks. I modified it to get attachments for each space and store them.

#!/usr/bin/python

import sys, string, xmlrpclib, re

server = xmlrpclib.ServerProxy('http://localhost:8080/rpc/xmlrpc')
token = server.confluence1.login('admin', 'admin')
space = 'test'
pagetree = server.confluence1.getPages(token, space) # Creates a list of dicts
allattachments = []
for pagedict in pagetree:
	pageid = pagedict['id'] # Pulls the ID from the current dict from the pagetree list
	pagetitle = pagedict['title']
	attachments = server.confluence1.getAttachments(token, pageid) # Creates another list of dicts
	d = {'title': pagetitle, 'attachments': attachments} # Adds attachment list to a temp dict
	allattachments.append(d) # Appends each new dict to the allattachments list 
# print the page title and all attachments and their file types from the first dict in the allattachments list
print allattachments[0]['title'] 
for attachments in allattachments[0]['attachments']:
	print attachments['title'], attachments['contentType']
exit('Done!')

The print at the end is just meant to be a quick example of how you can retrieve the various bits of data to suit your needs. If you ever want to know the keys in a dict, you can user list(dict) and it will print all the keys you can pull values from.

Joel Martinez December 10, 2012

Thank you, I appreciate your approach.

However, can't the pages themselves have children as well? Unless, the "getPages" call actually gets ALL pages, including every page descendant. Otherwise, I imagine you'd need a recursive solution to traverse to the bottom of the page hierarchy.

Adam Laskowski
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.
December 11, 2012

The getPages call does get all pages in a space, regardless of what level in the hierarchy they exist. The call itself is explained here:

https://developer.atlassian.com/display/CONFDEV/Confluence+XML-RPC+and+SOAP+APIs#ConfluenceXML-RPCandSOAPAPIs-Pages

1 vote
Pablo Garcia October 20, 2013

Hi!

Maybe this post is a little bit old (or too old) but I needed to retrieve all the fonts in our company's wiki, and your Python script was useful, however incomplete, as it only returns one page attachments (you use allattachments[0] and that's the problem). Also now is confluence2 object and, in my case, we don't use the port 8080.

Here is your code modified to my preferences (you can change the attachment filetype or just remove it):

#!/usr/bin/python
 
import sys, string, xmlrpclib, re
 
server = xmlrpclib.ServerProxy('http://localhost/rpc/xmlrpc')
token = server.confluence2.login('user', 'userPWD')
space = 'FONTS'
pagetree = server.confluence2.getPages(token, space) # Creates a list of dicts
allattachments = []
for pagedict in pagetree:
	pageid = pagedict['id'] # Pulls the ID from the current dict from the pagetree list
	pagetitle = pagedict['title']
	attachments = server.confluence2.getAttachments(token, pageid) # Creates another list of dicts
	d = {'title': pagetitle, 'attachments': attachments} # Adds attachment list to a temp dict
	allattachments.append(d) # Appends each new dict to the allattachments list 
# print the page title and all attachments and their file types from the first dict in the allattachments list
for each in allattachments:
	print each['title']
	for attachments in each['attachments']:
		if (attachments['contentType'] == 'application/x-font-ttf' or attachments['contentType'] == 'application/vnd.oasis.opendocument.formula-template'):
			print '...'+attachments['title'], attachments['contentType']
exit('Done!')

Regards,

1 vote
vincent martin August 8, 2013

Hi, I think that all attachments are not link to a page so is there an other way to get all those attachments than using getPages() function?

Thx

Adam Laskowski
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 8, 2013

Attachment objects store the pageid for the page they are associated with, which is why the script uses getPages() to populate a list of pageids:

https://developer.atlassian.com/display/CONFDEV/Remote+Confluence+Data+Objects#RemoteConfluenceDataObjects-attachmentAttachment

I don't know of a better way to get the attachments via the API. You could directly query the database as an alternative method by joining the content and attachments tables.

TAGS
AUG Leaders

Atlassian Community Events