Contentful logo

Contentful Community

Published or Draft?

#1

How can I find out whether content items are published or in draft status with the preview API?

1 Like

#2

Hi @christian.kissner,

That’s unfortunately not possible with our Preview API, but it is a possibility with our Management API.

Using our Management API, If you already have an entry, sys.publishedVersion indicates the version of the document that was published.

if sys.version = sys.publishedVersion + 1 then it means the document in the management API is exactly in the state as it was published.

if sys.version > sys.publishedVersion + 1 the it means that the document was published but there have been changes since the last time it was published.

In order to query from the api all entries that have a change you can use the changed=true parameter in the Content Management API calls.

Let me know if that makes sense or if you have other questions :slight_smile:

1 Like

#3

Hi @gabriel!

That’s unfortunate as you say, it would be a good piece of information to have in the preview api. Would it be worth making this into an enhancement?

Thanks for the answer

Cheers.

0 Likes

#4

Hey @christian.kissner,

I think there are some big considerations in implenting this, but our product team is already aware of your great feedback and I’ll update this thread in case it changes :blush:

Let me know if you have any other questions :slight_smile:

0 Likes

#5

Great, thanks!

Adding sys.publishedVersion to the response should be good enough since sys.version is already present.

Cheers

1 Like

#6

Hi @christian.kissner, you also could accomplish this by making a CDA call which might be easier, faster, and you wouldn’t be exposing a CMA token.

Query for entries and filter by the sys.id of the entry in question: https://cdn.contentful.com/spaces/<SPACE_ID>/entries?sys.id=<ENTRY.SYS.ID> and if the resulting response.items is empty (i.e., length is zero) it means the entry in question is draft. If the resulting response.items has one item and the sys.updatedAt properties of the CDA and CPA responses are the same this means the entry is published, and the sys.updatedAt properties are different this means the entry is published with pending changes. Here’s a table of the different statuses:

CDA & CPA sys.updatedAt same CDA & CPA sys.updatedAt different
response.items = 0 draft draft
response.items = 1 published published with pending changes

How does your preview frontend use this information? Normally users don’t really care if an entry is draft or published when previewing because they are editing a specific page, article, or post, etc. in Contentful and the preview link in the webapp links directly to the preview app’s view of that specific piece of content. I could see this information being helpful if a field value isn’t passing that field’s validation rules (which could happen with draft or updated entries) but this could also be mitigated with error handling in the frontend app.

1 Like

#7

Hi Charlie, thanks for looking into this!

Rather than sending users to the contentful CMS app first, we’d like to send them to the preview site directly so they get more of a WYSIWYG experience. We’d just inject edit icons that link to the contentful CMS app. In the screenshot below the red box alerts
the user that the entry hasn’t been published yet. Please excuse the design, it’s actually been replaced a few years back.

There certainly are workarounds for the missing draft status info in the preview API. And if you’re only looking at it in the context of the custom preview integration within the contentful cms app there’s of course no point. But there are other use cases.

Best

0 Likes

#8

Thanks for the context around what you are trying to do. I’ll certainly share your feedback with our product team and see if that might be something they’d add.

In the meantime you can use a small variation of what I suggested above to achieve the same result. I don’t know how exactly you are querying Contentful for the content on the example page you shared, but essentially you get a list of entries from the Preview API and make a list of the entry IDs and then query the Delivery API for entries where the sys.id is in that list (e.g., /entries?sys.id[in]=2lBPK3dEm84YOGIGUkEAi,1tnuVuJ6KUk84GMGGKoo68,1jteZMOi8eIoAQQWGOwIQy). The entry IDs that are only in the preview list are draft, ones that are in both lists and the sys.updatedAt property is the same are published and ones that are in both lists and the sys.updatedAt property is different are updated.

Here is a little javascript function I wrote to put a sys.status property on the list of entries:

function setEntryStatus(response) {
  let cpa_entries = response.items
  const cda_client = contentful.createClient({
    space: 'process.env.SPACE_ID',
    accessToken: 'process.env.CDA_TOKEN'
  })
  let cpa_ids = cpa_entries.reduce((a, c) => `${a}${a ? ',' : ''}${c.sys.id}`, '')
  return cda_client.getEntries({'sys.id[in]': cpa_ids}).then(response => {
    let cda_entries = response.items
    for (entry of cpa_entries) {
      let cda_entry = cda_entries.filter(i => i.sys.id == entry.sys.id)[0]
      if (cda_entry) {
        if (cda_entry.sys.updatedAt == entry.sys.updatedAt) {
          entry.sys.status = 'published'
        } else if (cda_entry.sys.updatedAt != entry.sys.updatedAt) {
          entry.sys.status = 'updated'
        }
      } else {
        entry.sys.status = 'draft'
      }
    }
    return cpa_entries
  })
}

Then you can use it like so:

var entries = []
preview_client.getEntries().then(setEntryStatus).then((items) => { 
  entries = items; 
  // now each entry will have a sys.status property
})

This would result in one extra CDA call but then you’d have a sys.status property that could determine the visibility of the edit icons.

hope this helps!

0 Likes

#9

@charlie, @gabriel – Thank you for providing these workarounds / solutions. I would also argue that the CPA (not just the Management API) should tell us if an entry has unpublished edits or not, and further more allow us filter by this info. Contentful UI allows us to filter entries by their status (i.e. Published, Changed, Draft, & Archived). I can’t think of why this info should not be returned by CPA.

I’d like to know if you have any suggestions for a problem I am trying to solve: I am trying to get a list of entries that are in the Changed or Draft state. We could have hundreds of published entries, however only a small number of them would be in the Changed or Draft state.

Going with the workaround you suggested means I would have to first make two API calls (one against CDA and one again CPA) requesting only the sys.updatedAt field for all entries in our space, compare the two list to come up with the small subset entries, and finally make a 3rd call against CPA to fetch the contents.
Using Management API would have other consequences (a second set of SDK to load, using a token that’s tied to a user, etc.).

Thank you

3 Likes

#10

What if an entry is in draft status because it has been unpublished or unarchived? Is there a way to fetch the unpublished or unarchived date?

0 Likes