Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Can Confluence REST API POST function create child page using ancestor's page title, not page id?

Andy Zhou June 20, 2018

Hello everyone,

According to the 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.

3 answers

1 accepted

1 vote
Answer accepted
Andy Zhou November 29, 2018

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("\\[|\\]","")
            }
        }
})
0 votes
Shaurya Chawla November 29, 2018

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

Andy Zhou November 29, 2018

According to Pages and blogs - Atlassian Documentation, it seems that blogs are for a space and do not have a parent page.  Pages do have the ancestry hierarchy.

0 votes
Pavel Potcheptsov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 21, 2018

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.

Andy Zhou June 21, 2018

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events