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

How can I, within code, attach a document to a page within our custom Confluence plugin?

Brian Alston April 24, 2015

I have never worked with Confluence until this new project I found myself on. We have a custom plugin that allows people to upload documents through the GUI, and add metadata about those documents within our plugin. Also, another function is we have an XML file being pushed to us through the Confluence SOAP service. This file is attached to a specific page within our plugin space, once a day.

 

We have now been charged  with turning the process around. We will now make a SOAP call to get the same XML file and attach it to the same page. Is there a way, within Confluence, to attach this XML document to the page without having to go through our own SOAP service?

 

I figured if I got the Page object, I could use the addAttachment() method to do this. However, as I said, I have no Confluence experience. I tried to create a quick test class to get the Page object, but I have no idea how to get the page ID or where I get the PageManager from. Whenever I try to google "attach document to confluence page", it seems like all of the articles have to do with going through the GUI, and I need to do this from the back end, through Java.

 

Any assistance would be greatly appreciated. Thank you, and have a good day.

2 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
Panos
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.
April 24, 2015

I am using something like...

Attachment attachment = attachmentManager.getAttachment(pageManager.getPage(space.getKey(),"Page title"),"Attachment name");
Attachment previousVersion=null;
if(attachment==null)
	attachment=new Attachment();
else
	previousVersion=attachment.copy();
attachment.setFilename(...);
attachment.setContentType(....);
attachment.setFileSize(...);
attachment.setContent(PAGE the attachment is attached to!!!!); //thats the trickiest part
attachmentManager.saveAttachment(attachment,previousVersion, INPUTSTREAM);

Hope it helps smile

Brian Alston April 24, 2015

It's definitely more than I had before ... thank you. What did you mean by "thats the trickiest part"?

Panos
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.
April 24, 2015

I mean that by looking the method setContent without consulting then documentation i was expecting some kind of inputstream. It took me a while to realize why i was getting exceptions ;)

Brian Alston April 28, 2015

The attachmentManager is coming back null. Any ideas?

Brian Alston April 29, 2015

Panos, never mind. I started using the DefaultAttachmentManager. Thanks for your help.

Panos
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.
April 29, 2015

Well, attachment manager should be injected in the constructor of the class as any xxxManager. Sorry for late reply just saw it :)

Brian Alston April 30, 2015

My apologies for the late reply, but Atlassian has limited me to 3 posts in a 24 hour period, so this has been my first chance to post again. Something weird was happening with my set-up and the AttachmentManager was not getting injected, therefore I thought I needed to use something like the DefaultAttachmentManager instead. After a complete reboot this morning, and nothing else changed, the AttachmentManager started being injected properly. I have no idea why this was happening, but everything appears to be working well now. Thank you so much for your help with this issue. Have a great day. :)

1 vote
Panos
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.
April 24, 2015

"I tried to create a quick test class to get the Page object, but I have no idea how to get the page ID or where I get the PageManager from."

You can get the PageManager and any xxxManager by injection:

public class MyClass{
	private final PageManager pageManager;
	public MyClass(PageManager pageManager){
		this.pageManager=pageManager;
	}
	....
	more methods
	....
}
TAGS
AUG Leaders

Atlassian Community Events