Can Confluence REST API POST function create child page using ancestor's page title, not page id?
Asked by Andy Zhou:

Hello everyone,

According to the [Confluence REST API examples](https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/ "Confluence REST API examples"), in order to create a child page, I need to have the ancestor page id. If I have just few ancestor pages, I could get the ancestor page ids manually ahead of time, then hard code the ids in the REST calls. But I have many spaces, each with the same structured page layout, and depending on certain condition, I need to create a child page to the same page title under different space. It'd be very difficult to get all the ancestor page ids ahead of time, then hard code into my REST call script, especially a groovy script in Jira transition post function.

Now the question is, can the ancestor's page title or webui link (instead of page id) be used combined with space key? Since ancestor's page title or webui link, plus space key would be able to uniquely identify the page in a Confluence instance, I think that would be possible.

In other word, instead of

```
??? space: [
??????? key: "SPACEKEY"
??? ],
???? ancestors: [
???????? [
???????????? type: "page",
???????????? id: "1234567",
???????? ]
???? ],
```

Can something like this work

```
??? space: [
 ??????? key: "SPACEKEY"
 ??? ],
 ???? ancestors: [
 ???????? [
 ???????????? type: "page",
 ???????????? title: "The Parent Page"
              },
 ???????? ]
 ???? ],
```

Or can something like this work

```
??? space: [
 ??????? key: "SPACEKEY"
 ??? ],
 ???? ancestors: [
 ???????? [
 ???????????? type: "page",
 ???????????? _links: {
                       webui: "/display/SPACEKEY/The+Parent+Page"
              },
 ???????? ]
 ???? ],
```

Thanks in advance.


Answers:
author: Andy Zhou
body: Well, I ended up making 2 REST calls, first to get the parent pageid from the pagetitle, then to create the child page. The groovy call to get the pageid is not as hard as I expected. Below is a sample.

```
def getPageInfo = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

getPageInfo
??? .createRequest(Request.MethodType.GET, "rest/api/content?title=" + pageTitle +"&spaceKey=" + spaceKey)
??? .execute(new ResponseHandler<Response>() {
??????? @Override
??????? void handle(Response response) throws ResponseException {
??????????? if(response.statusCode != HttpURLConnection.HTTP_OK) {
??????????????? throw new Exception(response.getResponseBodyAsString())
??????????? }
??????????? else {
??????????????? pageId = new JsonSlurper().parseText(response.responseBodyAsString)["results"]["id"].toString().replaceAll("\\[|\\]","")
??????????? }
??????? }
})
```


author: Shaurya Chawla
body: Is there a concept for ancestors in blog posts? Can we create child pages under a parent category/ancestor in similar way?

comments: author: Andy Zhou
body: According to [Pages and blogs - Atlassian Documentation](https://confluence.atlassian.com/doc/pages-and-blogs-320602215.html), it seems that blogs are for a space and do not have a parent page. Pages do have the ancestry hierarchy.


author: Pavel Potcheptsov
body: I think this won't work since you have to specify ancestor's page id in parameters of new page.

But you think that "Ok, instead of getting this value, confluence will do the job itself and all I need is to tell to confluence in which space and under which page the new page should be created."

Instead you have to get ancestors' ids by using SpaceKey/ID and page title.

comments: author: Andy Zhou
body: Thanks Pavel for the response, I know the latter two methods don't work as I've tried them, I'm going to reword the questions later.

But I wonder if something like the latter two would work, since both also uniquely identify the content with the combination of SpaceKey and page title (Note, in a space, there cannot be more than one page with the same title).

As you said, I could use REST call to get the page id from SpaceKey and page title, but this way, I have to call REST twice to create my child page:

* First REST call with SpaceKey and page title to get complete page info
* Parse the page info to get the page id
* Second REST call with SpaceKey and page id to post create child page

It makes to more difficult to code all these steps if you were to do this in a Jira transition post function.


Tags: child-page

rest-api