Copy page hierarchy in Confluence Server via REST API
Asked by c o:

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.


Answers:
author: Rodrigo Silva
body: 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,

comments: author: Danilo Yasushi Saita
body: Did you ever find it?


author: Rodrigo Silva
body: Hi [@Danilo Yasushi Saita](/t5/user/viewprofilepage/user-id/2901734) , 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](https://marketplace.atlassian.com/apps/22929/copy-page-tree?hosting=server&tab=overview)


author: Rodrigo Silva
body: <https://confluence.atlassian.com/doc/copy-a-page-139013.html>


author: Danilo Yasushi Saita
body: I have confluence 6.5.0.

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


author: Danilo Yasushi Saita
body: Ok, my bad. The api is private. I was looking at the public apis.


author: Patrick D?rig
body: I have confluence 7.13.2  

[@Danilo Yasushi Saita](/t5/user/viewprofilepage/user-id/2901734)Did you find a way to copy the page hierarchy using a rest api on the server version ?


author: Edwin Kyalangalilwa
body: Hi,

For server it'll be

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

comments: author: Danilo Yasushi Saita
body: This is very misleading. There is no such api in Confluence Server.


author: Francis Vila
body: [@Edwin](/t5/user/viewprofilepage/user-id/896950)I 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,


author: Akin A
body: [@Francis Vila](/t5/user/viewprofilepage/user-id/1178555) Found any way to copy page tree using any Confluence REST API?


author: Bartosz Dopke
body: 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(

    url, headers=headers, data=payload, auth=('myname@domain.com', 'PAT')

    )

print(response.status_code)
```


author: Steve Sadler
body: [@Francis Vila](/t5/user/viewprofilepage/user-id/1178555) were you ever successful in using that endpoint to copy a page hierarchy? If so, could you state the solution?


author: Andrey Tetyuev
body: 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.


Tags: rest