Is it possible to generate a confluence page directly from the source code?
Asked by George N Bouvier:

Trying to find a solution of importing a large amount of data from an excel source, and have it populate a confluence page with specific formatting already created. As we are quite used to creating markup files for other portions of the business, I thought one way to do this is to read in a valid XML/XHTML file that I create using a script and have that generate a confluence page (assuming the xml file in question meets the formatting criteria for confluence pages)

I have been able to view the storage format used for confluence pages, so I feel I can get a pretty good idea of how to generate a valid markup file for a page.

Question - is it possible to create a page strictly through importing the markup file?


Answers:
author: George N Bouvier
body: I will check out these solutions! Thank you for the response, this helps!


author: Radek Dost?l
body: With <https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html> it should be possible with REST API:

# <https://docs.atlassian.com/ConfluenceServer/rest/7.20.0/#content-createContent>

Although I must say this documentation is getting me dizzy.

I don't have any "create" example on my hands, but I wrote a script a few months back to update a specific page weekly, so I reckon this might be similar to your case.

```
info "Updating Confluence page .." |tee -a $DEBUGLOG

# I guess you can ignore this trash here, but I'm just leaving it in for reference..
nextVersion=$(expr $(($(cat $tmpDir/page.currentVersion) + 1)))pageTitle="$(cat $tmpDir/page.title)"
echo '{"id":"'"${CONFLUENCE_PAGEID}"'", "title": "'"$pageTitle"'", "version": {"number": '"$nextVersion"'}, "type": "page", "body":{"storage":{"value":"'"$(cat $tmpDir/page.body.storage-updated | sed 's#"#\\"#g')"'", "representation":"storage"}}}' > $tmpDir/put-data.jsonURL="${CONFLUENCE_INSTANCE}${RESTPATH_CONTENT}${CONFLUENCE_PAGEID}"

debug "URL: $URL" >> $DEBUGLOGdebug "pageTitle: $pageTitle" >> $DEBUGLOGdebug "nextVersion: $nextVersion" >> $DEBUGLOG
cat $tmpDir/put-data.json >> $DEBUGLOG

# And this is the PUT for update
curl -X PUT \? -b "$apiToken_Confluence" \? -s -S \? -H 'Content-Type: application/json' \? -d @$tmpDir/put-data.json \? --url "$URL" \? -o $tmpDir/put-response
```

And as you see from this, I did use the storage format. First I got the current page via GET - and I parsed the json response with python to get the title, body, and current version. Then I modified the body (so the body is in $tmpDir/page.body.storage-updated), and with that I then stitched the json back together as $tmpDir/put-data.json, and then I sent it back with PUT as an update.

Can't paste the whole bash script since it's technically proprietary and has a few "specific" data in it, but this snippet is generic enough to be pasted publicly.

Apart from REST, Confluence does have one option of importing files from the disk, and it should support storage format as well:

# <https://confluence.atlassian.com/conf59/import-a-text-file-792499637.html>

So if you prepped the pages and put them in a directory of your Confluence's choosing (it shows the directory in Space administration -\> Content Tools -\> Import), it should also work, and it should **replace** an existing page if the page name is the same, although I have no experience with this particular import page.


Tags: confluence-server

server