Macro tutorial for button functionality

Hello,

I try to create my first own macro.
So far, I’ve finished the “Hello World” marco.

My Marco is about:

With the help of a button the function “copy page tree” should be called. The function is passed parameters to copy a specific page including subpages. The target of the files is the page where the button is located.

Is there any tutorial on how to call functions via a button?
Please note, I’m a total beginner on Confluence development and my Java is a bit rusty.

Thank you in advance

If I understand correctly you would like to call a Java method on the server when a user clicks a button rendered by your macro on a Confluence page?

There is a bit of groundwork you need to lay to make this happen, you probably want to do something like the following:

  1. Create a REST endpoint in your app which when requested will call the “copy page tree” function.
  2. Include a JavaScript script when you render your macro so that when the button is clicked it triggers a request from the user’s browser to your REST resource.

Those are pretty abstract instructions, but might help guide your googling efforts or to come up with a more specific question.

Cheers,
Lorenzo

Hello Lorenzo,
Thank you, that has brought me a little further.
Now I have finished my REST endpoint and I want to call the function PageCopyEvent (Object source, Page origin, Page destination, ConfluenceUser initiator, boolean suppressNotifications).
What do I have to specify as an object source and the other parameters?

public class MyRestResource {

 @GET
 @AnonymousAllowed
 public Response getMessage (@QueryParam ("key") String key)
 {
 PageCopyEvent (Object source, Page origin, Page destination, ConfluenceUser initiator, boolean suppressNotifications);
 }

I would then make the call via a link
localhost: 5990/refapp/rest/myrestresource/1.0/message?key=some value

Hey Jörg,

so first off it looks like you’re missing the @Path annotation on your method (unless you put that on your class?.. but it really should be on the method).

Then your method signature says you want to return a response object so at the end of your method you want something like this:

String json = ...
return Response.ok(json, MediaType.APPLICATION_JSON).build();

Now to the PageCopyEvent. Events are usually thrown by Atlassian so they are meant to be listened to. Just creating them yourself does nothing at all (without publishing them). I guess what you actually want is to have something like the PageManager injected into your class and call its methods.

BTW: From the URL you provided it also looks like you’re running the refapp and not Confluence. If you want to develop a macro you probably want to do that on Confluence. :slight_smile:

Hi Sven,

ok, @Path I had in it. I am not sure if the method call has to give something back or just calls something.
I do not understand with the PageManager. Should my class implement that? Then what function should I call to get to the CopyPageTree function? I did not find any suitable function in the documentation.

Please can you give me a little code example! Please, I’m getting crazy about that all?

So I guess the root problem is that you don’t seem to know what dependency injection is. I think you should read this. I hope after that it becomes more clear how you get a pageManager instance in your class.

You will find that the PageManager has a getPage() and that Page objects have a copyLatestVersion().

Thanks! That may help me :slight_smile: