Forums

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

API to read/write tasks in Confluence?

Philip Colmer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2019

I've been asked to try and write a script to synchronise tasks between Confluence and Google. However, I can't find an API to read and/or write tasks in Confluence. Is there one?

Thanks.

 

3 answers

1 vote
Diego
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 23, 2019

Hello there Philip!

You sure can do this with Confluence REST API. For this to work we need to send a curl with the necessary content into your instance. For example:

curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"MY TASK PAGE","space":{"key":"CIAS"},"body":{"storage":{"value":"<p>This is a new page</p> <ac:task-list><ac:task><ac:task-status>incomplete</ac:task-status><ac:task-body>task list item</ac:task-body></ac:task></ac:task-list>","representation":"storage"}}}' http://yourconfluence.address/rest/api/content/ | python -mjson.tool

This is the resulting page after sending this curl to my local Confluence instance:

Screen Shot 2019-01-23 at 18.06.40.png                           

You need to run this curl command from a terminal, either Linux or macOS. This also works with Windows but you will need to install some software before being able to.

 

For further information on curl, you can check here:

How to cURL POST from the Command Line

Installing and using cURL

15 Tips On How to Use ‘Curl’ Command in Linux

 

What happens here is that you are POSTing to the server the page body as storage format string. With storage format, you can create structures using a simple syntax:

Confluence Storage Format - Lists

There is much more that you can do with REST API! You can check some working examples here:

REST API Examples

Knowing that, it is now a matter of choosing how you want to trigger this curl when tasks are added/removed.

Let us know if this does help your objective!

Philip Colmer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

Hi Diego

Thanks for this. It helps me understand how to add a task to an existing page but, unfortunately, it doesn't help me with retrieving tasks from Confluence.

I was hoping that since there is a My Tasks page (which clearly does have a way of pulling all of the tasks together) that there might be a programmatic way of achieving that outcome.

Diego
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 24, 2019

Hello there Philip!

You can also retrieve the page contents with REST API. The cURL needed would look like this:

curl -u admin:admin http://yourinstance.address/wiki/rest/api/content/<contentID>?expand=body.storage | python -mjson.tool

You can check more information about REST API here:

REST API Examples - Read content, and expand the body

You could set a cron job that calls the cURL to read a page content and then trigger another job to call the cURL to update said page.

How to Schedule tasks on Linux

The trickier part is when we need to compare both pages, from Google and Confluence. We can do that in Linux by comparing the Strings:

String Comparison

After you compare them, you can send the results to a variable and use it with the cURL to update the page. Much like how a payload would work.

Also, I would like to clarify that the page named MY TASK PAGE was set just as a placeholder in my last example.

Hope this helps out!

Philip Colmer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

Hi Diego

I appreciate that … although that would require me to scan every page in every space, something that the My Tasks page doesn't seem to need to do.

I will take it, therefore, that there isn't a cleaner way to retrieve tasks for an individual.

Thanks for the assistance.

Regards

Philip

Diego
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 25, 2019

Yes, this requires you to scan for every page that has tasks in it. However, each time you publish a page, a new database entry is generated in the CONTENT table in Confluence. Each entry has a different contentid value.

So, if you end up editing one of those pages with tasks, you will need to get their contentid again. But, as long as you keep the page name unique to your entire instance, you should be able to find it relatively easily by querying the database and matching the lowertitle column.

We have a similar situation where a customer needed to find a specific page version but the query should help you to build yours for this case:

Get required version of content properties via REST API

But yes, you are right. There is no real "clean" way of doing this. No native method or API is currently capable of doing this kind of integration and comparisons.

0 votes
David
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!
March 6, 2022
0 votes
Ilnur Gabdushev
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!
June 19, 2019

Have same issue - can't get task list via API.

@Diego I've tried your advice, but it doesn't help me. Details follows.

As you said, I've created a page with macro on it. I've done it manually via web-ui (browser), not REST API but I think it does not matter, because when I access this page via web-ui - it shows me a list of all inline tasks - i.e. works as expected.

But when I request this page via REST API as you said

/rest/api/content/<contentID>?expand=body.storage

it respond with

{
"id": "60265808",
"type": "page",
"status": "current",
"title": "tasks all",
"body": {
"storage": {
"value": "<p><ac:placeholder>Edit the Task Report macro to customise your report.</ac:placeholder></p><p><ac:structured-macro ac:name=\"tasks-report-macro\" ac:schema-version=\"1\" ac:macro-id=\"fdd20554-fe0a-424f-9a52-53fd7a6b8bbf\"><ac:parameter ac:name=\"reverseSort\">true</ac:parameter><ac:parameter ac:name=\"status\">complete</ac:parameter></ac:structured-macro></p>",
"representation": "storage",
"_expandable": {
"content": "/rest/api/content/60265808"
}
},
//...

i.e. - It returns macro markup, but not the result of its execution!

So how do i get inline tasks list?

 

For other who have same issue:

Please vote for feature request CONFSERVER-56023: REST API for inline-tasks

Ilnur Gabdushev
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!
June 21, 2019

Found that this expand=body.storage in

/rest/api/content/<contentID>?expand=body.storage

Must be expand=body.export_view:

/rest/api/content/<contentID>?expand=body.export_view

Then value will contain HTML with result.

Like esteege likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events