How to get my created subpage to display? (Created using a Java api Macro)

I have a macro which automatically runs when a project page is opened, it checks to see if all the subpages are there. If any are missing I call a create page function to generate it.

The page generated has the correct parent page according to the confluence homepage but when I go to the project page I cannot see it listed underneath.

This is causing an error to occur and confluence to crash because it still thinks it needs to generate this page, when it trys again it errors saying a page cannot be created with the same title.

Code snippet:

public String execute(Map params, String body, RenderContext renderContext) throws MacroException {

System.out.println("First print out......................");

if (!(renderContext instanceof PageContext)) {
throw new MacroException("This macro can only be used in a page");

Map<String, Object> context = MacroUtils.defaultVelocityContext();
PageContext pageContext = (PageContext) renderContext;
ContentEntityObject contentEntityObject = pageContext.getEntity();
long currentPageId = contentEntityObject.getId();
Page page = pageManager.getPage(currentPageId);

List<Page> pagesList = page.getChildren();
String direct = System.getProperty("user.dir") + "/pagelist.txt";
boolean flag = false;
Space ourSpace = page.getSpace();

try (BufferedReader br = new BufferedReader(new FileReader(direct))) {
String line = null;
while ((line = br.readLine()) != null) {
for (int i = 0; i < pagesList.size(); i++) {
String temp = pagesList.get(i).toString().replace("page:", "").split("v.")[0];
if (temp.contains(line)) {
System.out.println("Page: " + line + " found in subpages!");
flag = true;
if (flag == false) {
System.out.println("Page: " + line + " needs creating!");
String newString = line.trim();
Page missingPage = createPage(newString, ourSpace, page, 0);
flag = false;
} catch (Exception e) {
System.out.println("Threw an exception!!!!!!!!!!!!!!!!!!!!");

System.out.println("Macro ran");
// render the Velocity template with the assembled context*/
return VelocityUtils.getRenderedTemplate(MACRO_BODY_TEMPLATE, context);



private Page createPage(String title, Space space, Page parentPage, int position) {
Page subPage = new Page();


pageManager.saveContentEntity(subPage, null);

try {
Files.write(Paths.get("pagelist.txt"), (" " + title + "\n").getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {

return subPage;



0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,400 views 15 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you