Atlassian Confluence is one of the leading Team collaboration software in the market. It helps one to create, collaborate and organize work in one place. You can create nice document, dashboard etc. quickly and share with your team using it. Confluence can be either Cloud-based or Server based. You can learn more about Confluence from Atlassian documentations.
Cloud : https://confluence.atlassian.com/confcloud
Server : https://confluence.atlassian.com/doc/confluence-server-documentation-135922.html (make sure you select the correct version number based on your requirements).
I recently worked in a Project, where I had to write some automation scripts to work with Confluence. Obviously, I started looking for API documentations for Confluence and found some here. Oh! by the way, I was using Server based / Data center based Confluence infrastructure.
This is going to be a series of blogs and will try to share different script blocks I wrote to perform different operations on Confluence using REST API.
Today’s blog is mainly for Confluence Page management using Powershell.
Lets start with the API endpoint URL. To manage Confluence page and content the endpoint URL should look like :
$ConfluenceURL = "https://<confluence server>/rest/api/content"
As a prerequisite, you must have access to Confluence to manage confluence resources including pages, space, content, attachments etc.
You can create a PSCredential object to pass credentials. For simplicity, I am letting users to be prompted for credentials :
$cred = Get-Credential
Next, we need to setup the Authorization Header to use for the REST api call in subsequent steps.
$Headers = @{'Authorization' = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($cred.UserName+":"+[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($cred.Password)) )))
'X-Atlassian-Token' = 'nocheck'
}
If you have successfully executed above steps, you are all set for making api calls.
Let’s work through an example to understand the flow better. First, We are going to check if a Confluence page exists in a Space. If you already know the Page Id , you can use it search the page. If not, use Page Name and Space Key. You can find Page id by clicking on and then on Page Information. The Page Id should be visible in the URL address.Once you execute the script block, if the page exists, it will return page information, otherwise, will not return any result.
Search Confluence Page :
Check using Page Id :
$pageId = "123456"
$pageUriPath = "$($ConfluenceURL)/$($pageId)?expand=version"
$pageContent = Invoke-WebRequest -Method GET -Headers $Headers -Uri $pageUriPath | ConvertFrom-Json
if(!([String]::IsNullOrEmpty($pageContent))){
Write-Host "$($pageContent.version.number), $($pageContent.title)"
}
Check using Page Name(Title) and Space Key:
$pageTitle = "My Test Page"
$space = "AH"
$searchUri = "$($ConfluenceURL)?title=$($pageTitle)&spaceKey=$($space)&expand=history"
$result = Invoke-WebRequest -Method GET -Headers $Headers -Uri $searchUri | ConvertFrom-Json
if(!([String]::IsNullOrEmpty($result))){
Write-Host "$($result.version.number), $($result.title)"
}
Note, I am displaying page information on screen. You can store those in variables and use for further processing.
Create Confluence Page :
If the page does not exist, we need to create it. Following script block will help you creating a new Confluence page in specified Space. While creating this page we will add simple content to it. Confluence supports XHTML formatted content inside the page. You may refer to this article to know more about storage format.
Creating Confluence page is a POST call and it requires a formatted body to be submitted along with the API call.
Create Confluence Page:
$pageTitle = "My Test Page"
$space = "AH"
$body =
"{
`"type`":`"page`",
`"title`":`"$pageTitle`",
`"space`": {`"key`":`"$space`"},
`"body`":{
`"storage`":{
`"representation`":`"storage`",
`"value`":`"<p>This is <br/> a new page</p>`"
}
}
}"
Invoke-WebRequest -Method POST -Headers $Headers -Uri $ConfluenceURL -Body $body -ContentType "application/json" | ConvertFrom-Json
Create Child Confluence Page:
Sometimes you may need to create Child page inside an existing parent page. Following script block will help you on that.
$pageTitle = "My Test Page"
$space = "AH"
$pageId = "12345"
$body =
"{
`"type`":`"page`",
`"title`":`"$pageTitle`",
`"ancestors`":[{`"id`":$pageId}],
`"space`": {`"key`":`"$space`"},
`"body`":{
`"storage`":{
`"representation`":`"storage`",
`"value`":`"<p>This is <br/> a new Child page</p>`"
}
}
}"
Invoke-WebRequest -Method POST -Headers $Headers -Uri $ConfluenceURL -Body $body -ContentType "application/json" | ConvertFrom-Json
Finally, lets complete this blog with a script block to update content in a Confluence page. Page version number changes with every new change saved on the page. So, we need to identify the current version of the page and then increment it while making the api call. We captured page version number while getting page information earlier in this blog.We can use that information here.
Update Confluence Page Content :
$versionNo++
$pageTitle = "My Test Page"
$pageId = "12345"
$body =
"{
`"type`":`"page`",
`"title`":`"$pageTitle`",
`"version`":{`"number`":$versionNo},
`"body`":{
`"storage`":{
`"value`":`"<p>This is <b>Updated Content</b> in the Page</p>`",
`"representation`":`"storage`"
}
}
}"
$pageUriPath = "$($ConfluenceURL)/$($pageId)?expand=body.storage,version,space,ancestors"
Invoke-WebRequest -Method PUT -Headers $Headers -Uri $pageUriPath -Body $body -ContentType "application/json" | ConvertFrom-Json
If you are following along this blog you can now create page and manage content in Confluence page using Powershell and REST api.
I hope this blog will help you working with Confluence. Feel free to leave comment or suggestions in the comment section.