Search code examples
vbaexcelconfluenceconfluence-rest-api

Confluence: Update an existing page using VBA


I try to update a Confluence page using VBA. My idea was to load the content of the page using the REST API, modify the content and then upload the modified version.

Here is my code

Private Sub TestRESTApi()
Dim uname As String
uname = "XXXX" '
Dim pw As String
pw = "XXXX"


Set requester = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim ret As String
Dim send_str As String
With requester
    .Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Basic"
    .SetCredentials uname, pw, 0
    .Send
    ret = .responseText
    Debug.Print (ret)
End With

'Do replacement stuff here
'send_str = ret with replacements in the "value" part


Set sender = CreateObject("WinHttp.WinHttpRequest.5.1")
With sender
    .Open "PUT", "https://XXXX/rest/api/content/20057665", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Authorization", "Basic"
    .SetCredentials uname, pw, 0
    .Send send_str
    Debug.Print (.responseText)

End With
End Sub

The GET request works and i get back a json:

{
    "_expandable": {
        "ancestors": "",
        "children": "/rest/api/content/20057665/child",
        "container": "/rest/api/space/XXXX",
        "descendants": "/rest/api/content/20057665/descendant",
        "history": "/rest/api/content/20057665/history",
        "metadata": "",
        "operations": "",
        "restrictions": "/rest/api/content/20057665/restriction/byOperation",
        "space": "/rest/api/space/XXXX",
        "version": ""
    },
    "_links": {
        "base": "https://XXXX",
        "collection": "/rest/api/content",
        "context": "",
        "edit": "/pages/resumedraft.action?draftId=20057665&draftShareId=9c5eaf5c-d9c9-44d6-b7b6-317714f7581f",
        "self": "https://XXXX/rest/api/content/20057665",
        "tinyui": "XXXX",
        "webui": "/display/XXXX/Sandbox"
    },
    "body": {
        "_expandable": {
            "anonymous_export_view": "",
            "editor": "",
            "export_view": "",
            "styled_view": "",
            "view": ""
        },
        "storage": {
            "_expandable": {
                "content": "/rest/api/content/20057665"
            },
            "representation": "storage",
            "value": "THE CONTENT OF THE REQUESTET PAGE AS HTML"
        }
    },
    "extensions": {
        "position": "none"
    },
    "id": "20057665",
    "status": "current",
    "title": "Sandbox",
    "type": "page"
}

But when i try to update the page with the modified contetn, I get this error message:

{   
      "statusCode":400,   
      "data":   {
          "authorized":false,
          "valid":true,
          "errors":[],
          "successful":false   
      },   
      "message":"Must supply an incremented version when updating Content. No version supplied." 
}

But there is no current version of the page in the GET response. Where to i get the current version number from and where do i have to write the version number to?


Solution

  • I found the solution myself. When i request the current version of the page i have to add ,version to the request. (.Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage,version", False instead of .Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage", False). So i get information about the current version in the response:

       "version":{  
          "by":{  
             "type":"known",
             "username":"XXXX",
             "userKey":"XXXX",
             "profilePicture":{  
                "path":"/images/icons/profilepics/default.png",
                "width":48,
                "height":48,
                "isDefault":true
             },
             "displayName":"XXXX",
             "_links":{  
                "self":"https://XXXX/rest/experimental/user?key=XXXX"
             },
             "_expandable":{  
                "status":""
             }
          },
          "when":"2018-08-07T08:31:41.225+02:00",
          "message":"",
          "number":2,
          "minorEdit":false,
          "hidden":false,
          "_links":{  
             "self":"https://XXXX/rest/experimental/content/20057665/version/2"
          }
    

    And here i can increment the number and the conflunce server accepts my update.