How to create nested child pages / child page of a child page (Using Confluence API)

Ryan Duncan August 16, 2023

Hello, I am new to the confluence rest API and have been able to successfully create a page as a child page by populating the  

'ancestors': [{'id': target_page_id}]

My issue is i want to create a tree like such 

Home Page (Top - Level page)

  • Test Page 
    • Sub Test Page

I can create Test Page but I am unsure on how to create the Sub Test Page as Test Page does not have a pageID associated with it. Its possible to do this from confluence but I have not been able to figure this out from the use of the API. 

 

I am using python for this, from what i can understand it doesn't seem like confluences REST API gives the user much control over the POST capabilities regarding the child pages. 

 

EDIT : You are able to I suppose in multiple steps create two pages at the top level 

Home Page (Top Level) 

Test Page (Top Level)

Sub Test Page (Top Level) 

 

The using the move function in confluence rest API, move the Sub Test Page under Test Page as such 

Home Page (Top Level) 

Test Page (Top Level)

  • Sub Test Page (child page) 

 

Then using the copy tree hierarchy function in the REST api move this under Home Page (Top Level) to receive the final result of 

Home Page (Top - Level page)

  • Test Page 
    • Sub Test Page

 

However what if i wanted to do something four levels deep

 

Home Page (Top - Level page)

  • Test Page 
    • Sub Test Page
      • Sub Test Page 

 

This solution to the original problem I am aware of is not a solution to the bigger issue.

How do you target child pages ? Is there a way to do this at all using the rest API?

1 answer

1 accepted

0 votes
Answer accepted
Ryan Duncan August 17, 2023

To answer my own question and to do it in a way that's better explained than you might find on the web. 

Essentially children pages have a pageID associated to them however it is not shown in the url of the page like it is for the parent page. I looked all over Atlassian documentation about this and it never distinguished the two from each other.

How do you find the child pageID ? what you would want to do is shown here 

http://example.com/rest/api/content/{pageID}/child/comment?start=20&limit=10

The json object will return with the information in the string, for me the child pageID was similar to the parent pageID which is why I overlooked it at first. 

 

For me I am using python to achieve this and the atlassian-python-api package was not and did not have everything i was looking for (especially when copying template information).

 

*** Code after you find the child page ID *** 

import json
import requests

def create_child_page(self, target_page_id, new_page_title):
rv = True

# Create base URL for REST api
url = '{confluence_base_url}{contentApiUrl}'.format(confluence_base_url=conf.confluenceBaseUrl,
contentApiUrl=conf.contentApiUrl)

# Request body
data = {
'type': 'page',
'title': new_page_title,
'ancestors': [{'id': child_page_id}],
'space': {'key': ''enter your space key'},
'body': {
'storage': {
'value': 'Type Description Here',
'representation': 'storage',
}
}
}

data = json.dumps(data)

response = send_post_command(url, data, conf.headers, self.auth)

print(json.dumps(response.json(), indent=1))

if response is None:
rv = False

return rv

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events