Copy page hierarchy in Confluence Server via REST API

c o
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 20, 2017

Hi all,

I am trying to copy a page hierarchy from one space to another space via the Confluence REST API. In the Confluence Cloud API there exists

POST /wiki/rest/api/content/{id}/pagehierarchy/copy

(see https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-pagehierarchy-copy-post).

 

On the server version I am getting a 404 error when trying to use this interface (note that authorization is not the problem). Is this functionality only available in the cloud version?

 

PS: In the cloud version it worked fine.

2 answers

0 votes
Rodrigo Silva
Contributor
March 12, 2018

Hi,

I have the same need here (copy a page hierarchy to another space)... I've installed REST api browser, but it really seems like this function is not available on server ...

Any ideas, anyone?

Thanks,

Danilo Yasushi Saita March 20, 2019

Did you ever find it?

Rodrigo Silva
Contributor
March 20, 2019

Hi @Danilo Yasushi Saita , which version of Confluence are you using? It's now possible to copy page hierarchies, since 6.0 https://confluence.atlassian.com/doc/confluence-6-0-release-notes-844235309.html#Confluence6.0ReleaseNotes-6-0-2

For previous releases, you can use the addon https://marketplace.atlassian.com/apps/22929/copy-page-tree?hosting=server&tab=overview

Danilo Yasushi Saita March 21, 2019

I have confluence 6.5.0.

I am looking for a way to copy the page hierarchy using a rest api, not the ui.

Danilo Yasushi Saita March 21, 2019

Ok, my bad. The api is private. I was looking at the public apis.

Like Rodrigo Silva likes this
Patrick Dörig
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 8, 2021

I have confluence 7.13.2

@Danilo Yasushi SaitaDid you find a way to copy the page hierarchy using a rest api on the server version ?

0 votes
Edwin Kyalangalilwa
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.
December 20, 2017

Hi,

For server it'll be 

POST /rest/api/content/{id}/pagehierarchy/copy
Danilo Yasushi Saita March 20, 2019

This is very misleading. There is no such api in Confluence Server.

Like Ron Elazar likes this
Francis Vila February 25, 2020

@EdwinI tried to use the syntax you suggest, ie:

POST /rest/api/content/{id}/pagehierarchy/copy

 I get error 404. Using the python version, the syntax translates to:

url = "https://my.domain/confluence/rest/api/content/11111/pagehierarchy/copy"
headers = {"Content-Type": "application/json"}
payload = json.dumps( {  
"copyAttachments": True,  
"copyPermissions": True,  
"copyProperties": True,  
"copyLabels": True,  
"destinationPageId": "2222222",  
"titleOptions": {    
"prefix": "aaa",    
"replace": "",    
"search": ""  }} )
rrr = requests.request(   
   "POST",
   url,
   data=payload,
   headers=headers,   
   auth=("user", "password"),
)
print(rrr.text)

 The user I'm connected to has view page rights on the source space, and successfully created the destination space, using the relevant API. Logged in as that user, the copy operation works when using the UI.

I also tried with   "copyPermissions"False,  "copyProperties"False,  "copyLabels"False,

Akin A
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 2, 2021

@Francis Vila Found any way to copy page tree using any Confluence REST API?

Bartosz Dopke August 27, 2021

Hi, I'm using this command below and this works completely fine

import requests

import json

url = 'https://mydomain.atlassian.net/wiki/rest/api/content/pageId/pagehierarchy/copy'




headers = {

    'Content-Type''application/json',

}

payload = json.dumps({

    "copyAttachments"True,   

    "copyPermissions"True,

    "copyProperties"True,  

    "copyLabels"True,  

    "copyCustomContents"True,

     "destinationPageId""destinationPageId",

    })

response = requests.post(

    urlheaders=headersdata=payloadauth=('myname@domain.com''PAT')

    )

print(response.status_code)
Steve Sadler
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 22, 2023

@Francis Vila were you ever successful in using that endpoint to copy a page hierarchy?  If so, could you state the solution?

Andrey Tetyuev July 7, 2023

It seems that the confusion comes due to different versions of confluence:

  • At Confluence Cloud there are API functions to copy page or entire page-hierarchy (as shown in several comments above).
    This way is easy (if available, see below) but has limitation that source and destination locations should be on same confluence server.
    The source page being defined as ID within REST API path while destination page being defined via "destinationPageId" parameter given as part of data by the POST method.
  • At usual Confluence (i.e. not Cloud variant) such API as shown in examples above is not available (that's why error 404 - page not found - being returned).
    Here I can propose only one solution: use some scripting language (i.e. python) to:
    • download source page content.
      For example you can do it with curl:
      "YOURCURLPATH\curl.exe" -o "OUTPUT_PATH\output.tmp" -D- -u USER:PASSWORD -X GET -H "Content-Type: application/json; charset=utf-8; accept-charset=utf-8" --url "https://YOUR_SERVER/rest/api/content/SOURCE_PAGE_ID?expand=container,children,body,body.storage,version,children.attachment"
    • load stored output file (e.g. "output.tmp") as a json data
    • select required parts from there (i.e. ignore labels, etc. if needed). In general you need at least the content of "body.storage"
    • iterate via attachments of source page and download them too (you can filter only attachments really used within page content if needed)
    • attach downloaded files (i.e. attachments) to destination page and get their new IDs
    • Finally add to the destination page the content from source page with replacing there old attachments IDs by new ones.

The way with script is more universal: the source and destination could be at different confluence servers, so different user credentials could be used.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events