The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Upload local markdown file via Confluence REST API (PowerShell)

Antti Nousiainen
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!
November 21, 2023

I'm working on a PowerShell script that would allow me to upload a local Markdown file from a path, and create a Confluence sub-page under a parent page and display the file's contents as Markdown.

So far I'm able to create the sub-page under whatever parent or space I like, however, the Markdown isn't displaying as Markdown, just raw text.

I tried going over the REST API docs and tried using both representation types: storage (errors out) and wiki (works but doesn't shows as raw text).

Any ideas on what I'm doing wrong? Been stuck a while on this one...

Here's how the user inputs parameters for the script:

New-MarkdownSubPage -ConfluenceUrl "https://confluence.domain.corp" -SpaceKey "SPACE1" -PageTitle "Parent_Page" -FilePath "C:\Temp\markdown.md"

 

And here's the relevant bits of the script:

$parentPageUri = "$ConfluenceUrl/rest/api/content?spaceKey=$SpaceKey&title=$PageTitle"
$parentPage = Invoke-ConfluenceAPI -Uri $parentPageUri -Method "GET"
$parentPageId = $parentPage.results.id
$fileContent = Get-Content -Path $FilePath -Raw
$fileName = (Get-Item -Path $FilePath).Name

$pageData = @{
type      = "page"
title     = $fileName
space     = @{ key = $SpaceKey }
ancestors = @(@{ id = $parentPageId })
body      = @{
storage = @{
value = $fileContent
representation = "wiki"
}
}
} | ConvertTo-Json -Depth 100

$createPageUri = "$ConfluenceUrl/rest/api/content"
$createdPage = Invoke-ConfluenceAPI -Uri $createPageUri -Method "POST" -Body $pageData

 

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Benno Baumgartner
November 22, 2023

You need to convert the Markdown to Confluence storage Format. Maybe try an addon  https://marketplace.atlassian.com/apps/1211438/markdown-html-plantuml-latex-diagrams-open-api-mermaid 

Maybe a Library for this exists?

Regards Benno

TAGS
AUG Leaders

Atlassian Community Events