How to get all versions of Confluence page via REST API
Asked Answered
V

2

6

I would like to retrieve all versions of a page in Confluence. There is a call in the REST API that claims to return the history, but I have little luck with it.

/rest/api/content/{id}/history

When I call this I get a response along these lines:

{"latest": true,
    "createdBy": {
    "type": "known",
    "profilePicture": {
        "path": "/confluence/s/en_GB/.../profilepics/Avatar-14.png",
        "width": 48,
        "height": 48,
        "isDefault": false
    },
    "username": "[email protected]",
    "displayName": "First Last",
    "userKey": "userKey"
}, "createdDate": "2014-12-29T13:56:16.000+0100", "_links": {
    "base": "https://host.abc123.net/confluence",
    "context": "/confluence",
    "self": "host.abc123.net/confluence/rest/api/content/12345678/history"
}, "_expandable": {
    "lastUpdated": ""
}}

I know that this page has 17 versions in the history through the browser interface. I can't seem to get anywhere by adding any kind of expand= to the query either.

The documentation for Confluence REST API tells me that this call should:

Returns a full JSON representation of the content's history

If I turn to the "normal" REST API call to retrieve content, I can get some hint about the history in the version structure:

{"version": {
    "by": {
        "type": "known",
        "profilePicture": {
            "path": "/confluence/s/en_GB/5639/.../profilepics/default.png",
            "width": 48,
            "height": 48,
            "isDefault": true
        },
        "username": "some.other@abc123",
        "displayName": "Some Other",
        "userKey": "userKeyGuidThingy"
    },
    "when": "2015-01-30T16:00:09.000+0100",
    "message": "",
    "number": 17,
    "minorEdit": false
}      }

I am very keen on retrieving the other versions, numbers 1 - 16, but I can't see how..

I'd be most grateful for any help to get me unstuck.. :)

Villenage answered 30/3, 2015 at 8:5 Comment(1)
it seems this is curently not possible. I just upvoted this improvement request at Atlassian's JIRA. You can upvote it too to attract attention.Idolatrize
R
3

Here is an example:
Request:
http://localhost:8090/rest/experimental/content/7965952/version?expand=content
Response:

    {
"results": [
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:56:29.000+03:00",
"message": "more stuff",
"number": 6,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "current",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/display/S1/cla1",
"tinyui": "/x/AI15",
"self": "http://localhost:8090/rest/api/content/7965952"
},
"_expandable": {
"container": "/rest/api/space/S1",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/6"
}
},
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:56:12.000+03:00",
"message": "stuff",
"number": 5,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "historical",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/pages/viewpage.action?pageId=7965962",
"tinyui": "/x/Co15",
"self": "http://localhost:8090/rest/api/content/7965952?status=historical&version=5"
},
"_expandable": {
"container": "",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child?parentVersion=5",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/5"
}
},
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:39:28.000+03:00",
"message": "",
"number": 4,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "historical",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/pages/viewpage.action?pageId=7965961",
"tinyui": "/x/CY15",
"self": "http://localhost:8090/rest/api/content/7965952?status=historical&version=4"
},
"_expandable": {
"container": "",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child?parentVersion=4",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/4"
}
},
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:39:12.000+03:00",
"message": "",
"number": 3,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "historical",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/pages/viewpage.action?pageId=7965959",
"tinyui": "/x/B415",
"self": "http://localhost:8090/rest/api/content/7965952?status=historical&version=3"
},
"_expandable": {
"container": "",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child?parentVersion=3",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/3"
}
},
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:38:36.000+03:00",
"message": "",
"number": 2,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "historical",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/pages/viewpage.action?pageId=7965957",
"tinyui": "/x/BY15",
"self": "http://localhost:8090/rest/api/content/7965952?status=historical&version=2"
},
"_expandable": {
"container": "",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child?parentVersion=2",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/2"
}
},
{
"by": {
"type": "known",
"username": "admin",
"userKey": "40288ac65c33f555015c33fd0aed0000",
"profilePicture": {
"path": "/download/attachments/327681/user-avatar",
"width": 48,
"height": 48,
"isDefault": false
},
"displayName": "admin",
"_links": {
"self": "http://localhost:8090/rest/experimental/user?key=40288ac65c33f555015c33fd0aed0000"
}
},
"when": "2017-10-16T14:23:14.000+03:00",
"message": "",
"number": 1,
"minorEdit": false,
"hidden": false,
"content": {
"id": "7965952",
"type": "page",
"status": "historical",
"title": "cla1",
"extensions": {
"position": "none"
},
"_links": {
"webui": "/pages/viewpage.action?pageId=7965955",
"tinyui": "/x/A415",
"self": "http://localhost:8090/rest/api/content/7965952?status=historical&version=1"
},
"_expandable": {
"container": "",
"metadata": "",
"operations": "",
"children": "/rest/api/content/7965952/child?parentVersion=1",
"history": "/rest/api/content/7965952/history",
"ancestors": "",
"body": "",
"version": "",
"descendants": "/rest/api/content/7965952/descendant",
"space": "/rest/api/space/S1"
}
},
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version/1"
}
}
],
"start": 0,
"limit": 100,
"size": 6,
"_links": {
"self": "http://localhost:8090/rest/experimental/content/7965952/version?expand=content",
"base": "http://localhost:8090",
"context": ""
}
}

this will get you all versions of a page with version messages

Rahmann answered 18/10, 2017 at 7:17 Comment(0)
D
0

I found the way to get version number.

so If you go to https://confluence.yourdomain.com/rest/api/content/page_id(1234567)/history. You will get the following json response.

{
   "previousVersion":{
      "by":{
         "type":"known",
         "profilePicture":{
            "path":"image/path",
            "width":48,
            "height":48,
            "isDefault":true
         },
         "username":"username",
         "displayName":"password",
         "userKey":"userkey"
      },
      "when":"date-time",
      "message":"",
      "number":1,
      "minorEdit":false
   },
   "lastUpdated":{
      "by":{
         "type":"known",
         "profilePicture":{
            "path":"image/path",
            "width":48,
            "height":48,
            "isDefault":true
         },
         "username":"username",
         "displayName":"password",
         "userKey":"userkey"
      },
      "when":"date-time",
      "message":"",
      "number":2,
      "minorEdit":false
   },
   "latest":true,
   "createdBy":{
      "type":"known",
      "profilePicture":{
         "path":"image/path",
         "width":48,
         "height":48,
         "isDefault":true
      },
      "username":"username",
      "displayName":"password",
      "userKey":"userkey"
   },
   "createdDate":"userkey",
   "_links":{
      "base":"https://confluence.yourdomain.com",
      "context":"",
      "self":"https://confluence.yourdomain.com/rest/api/content/page_id/history"
   }
}

From that response json just fetch the version number depends on the scripting language that you are using. For Python following the piece of code will fetch the version number.

version=requests.get(https://confluence.yourdomain.com/rest/api/content/page_id(1234567)/history)
version= version.json()
print version['lastUpdated']['number']

This will give you latest version number of the page.

Deyoung answered 19/4, 2016 at 0:38 Comment(1)
the answer only gets the last & the previous , meaning if you have 3 and above you won't get a full response :(Rahmann

© 2022 - 2024 — McMap. All rights reserved.