Problem getting content on different versions

Guys, I’m having some trouble while getting a content on different versions in the master branch.

My problem is that I need to get a published content on the current version and also get the same content on its previously published version to compare a specific field.

I changed the data.title of a content on Content Studio and published it, when I get the last 100 versions of this content (contentLib.get({key: 'id', version: 'versionid'})), all of them have the new value for data.title. It looks like it’s not getting the content on the specified version.

The weird thing is that I went to Data toolbox and listed the versions of this same content and everything seems to work there.

TEST CODE THAT I’M USING:

exports.execute = function (event) {
  
  Context.run(
    {
      repository: 'com.enonic.cms.default',
      branch: 'master',
      user: {
        login: 'su',
        idProvider: 'system'
      },
      principals: ['role:system.admin'],
      attributes: {
        ignorePublishTimes: true
      }
    },
    () => {
      if (event.type === 'node.pushed') {
        const contentsPublished = event.data.nodes

        // get the id of the first content of the published contents
        const contentId = contentsPublished[0].id

        const repo = Node.connect({
          repoId: 'com.enonic.cms.default',
          branch: 'master'
        })

        // get the first 100 versions
        const contentVersions = repo.findVersions({
          start: 0,
          count: 100,
          key: contentId
        })

        for (let index = 0; index < contentVersions.hits.length; index++) {
          const element = contentVersions.hits[index]

          const contentOnVersion = Content.get({ key: contentId, version: element.versionId })

          // it always printing the same title
          log.info(contentOnVersion.data.title + ' index: ' + index)
        }
      }
    }
  )
}

Enonic version: 7.6
OS: ubuntu 20

I don’t think its possible to get a specific version with the content api. Content.get() does not suport ids with versions. Looks like it simply ignored the version part of the id and get the main content.

Try using the node api instead. repo.get should be able to get version data since it does it on a lower level. If that does not work then, the versions are not supported by the api.

Well, isn’t that what the attribute version is supposed to do?

https://developer.enonic.com/docs/xp/stable/api/lib-content#get

That is what it should do. I’ll file a bug report. Link

1 Like

Thanks. By the way, my colleagues made a java version using com.enonic.xp.node.NodeService and it worked.

This was resolved quickly.
Seems the param was named wrongly in the docs:
Its not version its versionId (capital ‘i’)

Content.get({ key: contentId, versionId: element.versionId })

With the code provided added the correct param above

2 Likes