Is there a way to automate the Move page option
Asked by Casey Maynard:

I have pages that once completed I would like to move from one location to another location. And it is always the same location, can I set up a 'MOVE' button on the exiting page to click and it would move the page (and all of its child page too) from current location to the new location?

NOTE: Yes the Move option is VERY simple to use, but I do this action quite often so I just want a button to 'automate' the process.


Answers:
author: Zak Laughton
body: Hi Casey!

I was playing around with this, and think I got to the point that's REALLY close. The user macro below will create a button that is intended to move the current page under another page. The problem is, instead of waiting for the button to be clicked, it just moves the page as soon as the page is loaded with the macro.

It seems like the movePageAsChild() method is called as soon as it is read inside the button tag. If someone could find a way to not have it called until the button is pushed, this macro would be perfect! Unfortunately, I couldn't find a way as I was experimenting.

Putting this code here in case it helps you or anyone else on the way to a solution.

**ALMOST-WORKING** **CODE:**   
*(change "65538" to match the ID of your destination parent page)*

```
## Macro title: Move page
## Macro has a body: N
## Body processing: No macro body
## @noparams

#set ($pageManager = $action.getPageManager())

#set ($page = $content.getEntity())
#set ($destinationPage = $pageManager.getPage(65538))

<button class="aui-button" onclick="{$pageManager.movePageAsChild($page, $destinationPage)}">Move page under "$destinationPage.getTitle()"</button>
```

comments: author: Henrik Juel Petersen
body: Did you ever get this to work?


author: Casey Maynard
body: Well the code above works as described. The page is moved once loaded, but that is not realistic. It may be worth posting the question about how to prevent the move until a condition is met? So maybe there could be a checkbox condition on the page that the user has to have checked before it is moved? If the checkbox is not check the code would not move the page, and if the checkbox is checked then reload the page and it will be moved?


author: Henrik Juel Petersen
body: Sounds like a valid way to do it - but no matter how I add that movePageAsChild function to a user macro - its triggered instantly when the page is loaded.


author: Casey Maynard
body: There might be some ideas in this examples in the link below of how to put in 'conditions' like a checkbox is checked before move is allowed to happen.

Apologies as I am not of a 'developer's mind' so I am not able to understand how to modify the code to set up a conditional 'if statement'

<https://www.codota.com/code/java/methods/com.atlassian.confluence.pages.PageManager/movePageAsChild>


author: Zak Laughton
body: I've learned quite a bit about Javascript since I made this original post. I have some ideas that might help, but unfortunately, I don't have a Confluence environment still set up to test and confirm.

As I wrote the code in my comment above, Javascript sees the "onClick" value as a function call, not as a function definition, which is why it runs as soon as the button is put on the page. You might be able to fix this by wrapping the function call inside another function. To do this, change the last line to:

```
<button class="aui-button" onclick="{() => $pageManager.movePageAsChild($page, $destinationPage)}">Move page under "$destinationPage.getTitle()"</button>
```

Metaphorically speaking, if my original code is handing an instruction card to the button saying "Do this now!", then adding "() =\>" before the function call is passing an envelope to the button and saying "In case you get clicked, open this up and follow the instructions on the card inside." (See also: [Arrow function expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)).

If that still doesn't work, there's some good discussion here about adding functions inside "onClick": [How to call a function onclick and pass arguments](https://github.com/riot/riot/issues/1001).

*The only caveat I can think of here being that everything I wrote above addresses how Javascript works on a basic HTML page, while user macros are written with [Velocity templates](https://developer.atlassian.com/server/confluence/velocity-template-overview/), which might handle these calls differently.*


author: Casey Maynard
body: PM me and I can test it in my test environment and post the results once working


author: Henrik Juel Petersen
body: Hi [@Zak Laughton](/t5/user/viewprofilepage/user-id/311502)   

I've tested your suggestion - it still instantly executes the click functionality.

/Henrik


Tags: atl-adm

atlassian-marketplace