Confluence API - How to order content by lastmodified date?
Asked by Ben Loseby:

Hiya All,

I am currently trying to integrate with the Confluence API, to get wiki content pages to be used in a separate application. As part of this integration, I need to query content pages, and get pages updated within the last 24 hours, which is where to issue is occurring. I am getting errors when attempting to order by, or filter by the last modified date.

I have outlined more details below.

The Goal:

* Using the below endpoint, get content and order the results by the last modified date (For example history.lastUpdated.when, or version.when).
  * Alternatively, limit the results so the results only have a last modified date within last 24 hours.
* (API Docs) <https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content/#api-wiki-rest-api-content-get>

The Issue:

* When using the below call, I am seeing a 400 error with the message 'com.atlassian.confluence.api.service.exceptions.BadRequestException: Unsupported orderBy field: history.lastUpdated desc'.
  * https://{{my domain}}.atlassian.net/wiki/rest/api/content?expand=history.lastUpdated\&orderby=history.lastUpdated desc
* An alternative approach that was tried was the search endpoint, and CQL, but this returns another error, detailed below.
  * https://{{my domain}}.atlassian.net/wiki/rest/api/content/search?cql=type = page and lastmodified \> startOfWeek()
  * Error: "com.atlassian.confluence.api.service.exceptions.SSStatusCodeException: CQL was parsed but the search manager was unable to execute the search. Error message: com.atlassian.confluence.api.service.exceptions.SSStatusCodeException: There was an illegal request passed to XP-Search Aggregator API : HTTP/1.1 403 Forbidden"

Any assistance is much appreciated.


Answers:
author: Ben Loseby
body: After more testing an experimentation, I have managed to achieve the desired goal using the search endpoint. The original issue was due permissions on the account making the call.

When using another account with increased permissions, I was able to make the below call, using an encoded object for the query itself.

https://{{my domain}}.atlassian.net/wiki/rest/api/content/search?limit=25\&start=0\&cqlcontext=%7B%22contentId%22%{{content ID}}%2C%22contentStatuses%22%3A%5B%22current%22%5D%2C%22spaceKey%22%{{space key}}%7D\&cql=type%3Dpage%20and%20lastModified%3Enow%28%27-1d%27%29\&expand=body.storage%2Cversion

pre-enconding = {"contentId":{{content id}},"contentStatuses":\["current"\],"spaceKey":{{space key}}}


author: Juan Carlos Vera
body: hi [@Ben Loseby](/t5/user/viewprofilepage/user-id/5175751) ,

try with something like this:
>
> ```
> https://{{your domain}}.atlassian.net/wiki/rest/api/content?orderBy=lastmodified&expand=history
> ```

comments: author: Ben Loseby
body: Hi [@Juan Carlos Vera](/t5/user/viewprofilepage/user-id/4793417) ,

Thanks for getting back to me.

I have just tried the above, and whilst it does not error, the API seems to be ignoring the orderBy, and still order the results by createDate in ascending order.

I tested by trying to order using the createdDate, with the below, and even then it seems to ignore the direction specified:

* /wiki/rest/api/content?orderby=createdDate%20Desc\&expand=history
  * Note: "orderby" uses lowercase b
  * Attempted with both "desc" and "Desc"
  * Errors claiming "Unsupported orderBy direction: createdDate Desc"
* /wiki/rest/api/content?orderBy=createdDate%20Desc\&expand=history
  * Note: "orderBy" uses uppercase B
  * Attempted with both "desc" and "Desc"
  * Doesn't error but order by direction is ignored
* /wiki/rest/api/content?orderBy=lastmodified%20desc\&expand=history
  * Note: "orderBy" uses uppercase B
  * Attempted with both "desc" and "Desc"
  * Doesn't error but order by direction is ignored
* /wiki/rest/api/content?orderby=lastmodified%20desc\&expand=history
  * Note: "orderby" uses lowercase b
  * Attempted with both "desc" and "Desc"
  * Errors claiming "Unsupported orderBy field: lastmodified desc"


Tags: api

cloud

confluence-cloud

filter

page

sort