[SOLVED] restAPI, get content of a collection

Is there a way to get all items in a collection with restApi ?

See REST-API Content types - Collection

You can test it by creating a Collection as follows:

SITE_URL=https://6-classic.demo.plone.org/en
USER=admin:*****

MY_COLLECTION_AT_ID=$(
    curl \
        -s -X POST ${SITE_URL} \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        --data-raw '{"@type":"Collection","title":"MyCollection","query":[{"i": "review_state","o":"plone.app.querystring.operation.selection.any","v":["published"]}]}' \
        --user ${USER} \
    | jq -r '."@id"' \
    )

echo $MY_COLLECTION_AT_ID

# -> https://6-classic.demo.plone.org/en/mycollection

And requesting the collection with

curl \
    -s -X GET ${MY_COLLECTION_AT_ID} \
    -H "Accept: application/json" \
    --user ${USER} \
    | jq -r '."items"'

You'll get the items:

[
  {
    "@id": "https://6-classic.demo.plone.org/en",
    "@type": "LRF",
    "description": "",
    "image_field": null,
    "image_scales": null,
    "review_state": "published",
    "title": "English"
  },
  {
    "@id": "https://6-classic.demo.plone.org/en/frontpage",
    "@type": "Document",
    "description": "The ultimate Open Source Enterprise CMS",
    "image_field": null,
    "image_scales": null,
    "review_state": "published",
    "title": "Welcome to Plone"
  },
  ...

If you need the full objects add the parameter ?fullobjects to your query:

curl \
    -s -X GET ${MY_COLLECTION_AT_ID}?fullobjects \
    -H "Accept: application/json" \
    --user ${USER} \
    | jq -r '."items"'

Thanks.

I put the dart code here for later reference (since it is similar to other languages):

if (portalType == 'Collection') {
    apiListing = await http.get(
      Uri.parse(
          '${path_to_collection}/?metadata_fields=url&metadata_fields=icon&b_size=200),
      headers: <String, String>{
        'Accept': 'application/json,',
        'Content-Type': 'application/json'
      },
    );

see Requests: HTTP for Humans™ — Requests 2.28.1 documentation

Thanks, might be useful, but for this case I am actually making a 'Flutter Android App' (World Places — Grieg Medialog APP api if you want to try). Which gets content from a Plone site via restAPI ( Sri Lanka — Grieg Medialog APP api ) . It is just a 'hobby project', so if anyone wants to add 'their own places', I will give a login.

Is there a way to know if content in a collection has been updated (via restApi)?

I only want to update (my app) when something has changed (in a Plone Collection)

You mean changes in the definition of the Collection (a), changes in the resultset (b) or changes in the returned items (c)?

If some item in a folder changes or are added, the modify date changes, and I fetch again (from the App), but for a Collection, I am not sure how I can know if for example anothere are added (or removed) (=c).

If the collection itself is changed (=modified date is changed), it works now (=a).

Of course, I can fetch all items and compare them to the existing, and if they differ, I fetch all fields, but it will slow down things 'a bit'.

Since a Collection is kind of a query it is not aware of the status of its result set nor of the items in this result set.

Even if you get the same number of items in the result you cannot be sure if an item has been deleted and a new item has been added.

For your use case you'll probably need to put some logic in your client to check if the result set needs to be updated.

The endpoint @search could be helpful in such cases.

Can you 'search' within a Collection (I tried this in 'the beginning without luck' )

The only logic I can think of is 1) get the modify dates of all the items in the collection and store 'the newest' 2) Compare this with the last 'newest' date. (My collection is sorted alphabetically, if it was sorted on modified date one could maybe just 'batch=1'

Not for what you need.

You must anyway get the necessary information to decide if you must update or not. Either via Collection's items or via an extra query with @search.

The minimal algorithm is to have a set of the items in the client (with id and change_date) and get a set of the items from the server. Then calculate the set difference (e.g. in dart with Set.difference). If the difference is not an empty set, then update either the different items or the whole set in the client.