1

I am attempting to use the Confluence REST API to correct the tree structure of pages on a Confluence site. I can do this manually, however there are quite a few pages and I already have the tree structure in another XML file.

Currently I am able to upload pages and images using the confluence REST API. Definitions are located here.

I have attempted to use the /rest/api/content/{id} PUT command. This requires the content to be in the body of the request. Attaching the content I had received from a previous GET request I find I have to update the version count.

The above all works. Adding to the ancestor list however doesn't seem to. If I add the parent page to the list of ancestors I get formatting errors reported back.

Added to this if I manually attach the page to another page and request its contents I find that the parent appears twice in the ancestor list. Also the history count does not increase.

The resulting JSON returned from a GET request for the page:

http://someconfluencesite.com/rest/api/content/10031361?expand=ancestors (200)

{
    "id" : "10031361",
    "type" : "page",
    "title" : "Automatic Action Usage Updates",
    "ancestors" : [{
            "id" : "10031327",
            "type" : "page",
            "title" : "Action Lists"
        }, {
            "id" : "10031327",
            "type" : "page",
            "title" : "Action Lists"
        }, {
            "id" : "10031328",
            "type" : "page",
            "title" : "Actions Tab"
        }, {
            "id" : "10031327",
            "type" : "page",
            "title" : "Action Lists"
        }, {
            "id" : "10031327",
            "type" : "page",
            "title" : "Action Lists"
        }, {
            "id" : "10031328",
            "type" : "page",
            "title" : "Actions Tab"
        }
    ]
}

The tree this is for looks like the following:

  • Space
    • Action Lists
      • Actions Tab
        • Automatic Action Usage Updates

This leads me to believe I am using the wrong command.

I have attempted to contact Confluence support however their authorization emails are not arriving in my inbox. So I have come here :)

So my question is what is the REST call for creating a page tree in confluence? After that, what is the format of the request body?

Edit: The following PUT request gets a successful return code. Yet the returned object does not have the ancestor attached (not unusual as in all GET requests you have to expand to get it). The version number does not update. The two pages are not hooked to each other either.

{
     "id":"10552520",
     "type":"page",
     "title":"Correct Page Title",
     "ancestor":[
     {
         "id":"10552522",
         "type":"page"
     }],
     "version":
     {
          "number":"2"
     }
}

The nice thing the above does though is delete all the contents of the page.

The following POST call results in the page being created but having no ancestors. The ancestor exists with the id supplied. Strangely this also is created without any content in the page.

{
    "type":"page",
    "title":"Correct Title",
    "space":{"key":"SpaceKey"},
    "ancestor":[{"id":"10553655","type":"page"}],
    "body":{"storage":{"value":"<p>New Page </p>","representation":"storage"}}
}

Putting the above into the REST API browser also results in the children not being attached to the parent.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Jason
  • 840
  • 11
  • 23
  • 1
    Possibly of relevance: https://answers.atlassian.com/questions/5278993/updating-a-confluence-page-with-rest-api-problem-with-ancestors and https://jira.atlassian.com/browse/CRA-487. I understand that your question is about fixing the hierarchy of existing pages, but in the future, it's probably easier to just [create them in the correct location](http://stackoverflow.com/questions/23523705/how-to-create-new-page-in-confluence-using-their-rest-api/23526357#comment36097279_23526357) in the first place. :-) – Scott Dudley Feb 12 '15 at 02:30
  • 1
    According to the Confluence sources, the PUT command *should* be capable of moving a page to another parent. What exactly are the "formatting errors" you are getting? As far as I can see, in order to move page, the ancestors list should contain a single reference to the pageId of the desired parent page. – Scott Dudley Feb 12 '15 at 02:40
  • @ScottDudley Uploading the correct tree structure initially would be best. I just can't understand how to get the ancestor list correct. I have attached the JSON returned to me from an already constructed tree (I have reduced it for brevity). I can't see how it relates to the tree I have. What would be the JSON for the tree I have listed? – Jason Feb 12 '15 at 22:44
  • Performing the [link]( http://stackoverflow.com/questions/23523705/how-to-create-new-page-in-confluence-using-their-rest-api/23526357#comment36097279_23526357) create them in the correct location results in the tree only being one deep. To get the nesting all the ancestors need to be there it would seem. – Jason Feb 12 '15 at 22:47
  • @ScottDudley I would like to also say thanks for the links, great help. I understand the ancestor field a bit more now. Yet still not able to format the rest request correctly it would seem. – Jason Feb 12 '15 at 22:52
  • What IDs are you passing such that the tree only ends up one level deep? AFAIK you only need to provide one ancestor when using POST to create a page, which should be the ID of its direct parent. So, to attach a child of Automatic Action Usage Updates, you would specify an ancestor of ..361. A new child of Actions Tab should specify an ancestor of the 328 value, just as the Actions List children should have an ancestor ID ending in 327. Are you saying that this is what you are doing, but it attached the pages elsewhere? – Scott Dudley Feb 13 '15 at 01:01
  • @ScottDudley I will give this a go again. From memory I couldn't do this as I always go formatting errors just supplying the ID part for ancestors. The only way I could around this was to get the child JSON, then get the parent JSON, then add the parent to the ancestors list of the child. I will simply attempt to add the parent ID to the list of ancestors. – Jason Feb 16 '15 at 01:42
  • @ScottDudley attached the result of what you have suggested. If I don't increase the history number the PUT fails with an error stating the history number needs to be increase. If I don't include the title for the page I get an error saying page with title "" already exists. The last update is where I stop getting errors no parentage is updated. I am really at a loss as to how this works for moving pages around. – Jason Feb 17 '15 at 00:40

3 Answers3

1

So it appears the answer to my question is in the question itself.

The JSON needed to have "ancestors" not "ancestor" as the array name for the ancestors array. Once this was changed it all works for a POST request.

So if your reading this and having similar issues make sure that all the element names in the JSON your passing are correct. If they aren't, they simply are ignored.

Jason
  • 840
  • 11
  • 23
  • 1
    In my case, the "ancestors" property had to be specified before the "body" property. If I put "ancestors" at the end, it was ignored. – xorcus Dec 15 '19 at 21:38
1
{
    "id":"10552520",
    "type":"page",
    "title":"Correct Page Title",
    "ancestors":[
        {
            "id":"10552522",
            "type":"page"
        }
    ],
    "version":
        {
            "number":"2"
        }
    }

It's "ancestors", not "ancestor".

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
angelina
  • 11
  • 1
0

To move an existing page first get it's version, space, etc. Then call PUT with version + 1, space, ancestors, title.

pageaschild = { "type":"page",
      "title":name,
      "space":{"key":space},
      "ancestors":[{"id":ancestorid,"type":"page"}],
      "version":{"number":version}};

$.ajax({type:'PUT', url:baseURL + pageId, contentType:"application/json;charset=utf-8", data:JSON.stringify(pageaschild)});
Rimma Shafikova
  • 416
  • 5
  • 7