Getting File Version History - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

To get the version history for a file use the VersionsApi.listVersionHistory method, which will retrieve a list of all the node versions.

For more information about this ReST API endpoint, see Get File Version History.

For a description of the common parameters, such as include, see Common Parameters.

import org.alfresco.core.handler.VersionsApi;
import org.alfresco.core.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class ListVersionHistoryCmd {
    static final Logger LOGGER = LoggerFactory.getLogger(ListVersionHistoryCmd.class);

    @Autowired
    VersionsApi versionsApi;

    public void execute(String fileNodeId) throws IOException {
        VersionPagingList nodes = listVersionHistory(fileNodeId);
    }
    
    /**
     * List the version history for a file node.
     *
     * @param fileNodeId the id of the file node
     * @return a list of child node objects contained in the folder, or null if not found
     */
    private VersionPagingList listVersionHistory(String fileNodeId) {
        Integer skipCount = 0;
        Integer maxItems = 100;
        List<String> include = null;
        List<String> fields = null;

        LOGGER.info("Listing versions for file node ID {}", fileNodeId);
        VersionPagingList result = versionsApi.listVersionHistory(fileNodeId, include, fields, skipCount, maxItems).getBody().getList();
        for (VersionEntry versionEntry: result.getEntries()) {
            LOGGER.info("Node version " + versionEntry.getEntry().toString());
        }

        return result;
    }
}

We would execute this command class something like this, passing in the file Node ID:

% java -jar target/rest-api-0.0.1-SNAPSHOT.jar list-file-versions 0492460b-6269-4ca1-9668-0d934d2f3370                   

2021-04-29 08:04:48.145  INFO 18326 --- [           main] o.a.tutorial.restapi.RestApiApplication  : Started RestApiApplication in 6.498 seconds (JVM running for 7.686)
2021-04-29 08:04:48.148  INFO 18326 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[0]: list-file-versions
2021-04-29 08:04:48.152  INFO 18326 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[1]: 0492460b-6269-4ca1-9668-0d934d2f3370
2021-04-29 08:04:48.152  INFO 18326 --- [           main] o.a.t.restapi.ListVersionHistoryCmd      : Listing versions for file node ID 0492460b-6269-4ca1-9668-0d934d2f3370
2021-04-29 08:04:48.990  INFO 18326 --- [           main] o.a.t.restapi.ListVersionHistoryCmd      : Node version class Version {
    id: 3.0
    versionComment: null
    name: somestuff2.txt
    nodeType: acme:document
    isFolder: false
    isFile: true
    modifiedAt: 2021-04-28T12:44:51.578Z
    modifiedByUser: class UserInfo {
        displayName: Administrator
        id: admin
    }
    content: class ContentInfo {
        mimeType: text/plain
        mimeTypeName: Plain Text
        sizeInBytes: 30
        encoding: ISO-8859-1
    }
    aspectNames: null
    properties: null
}
2021-04-29 08:04:48.990  INFO 18326 --- [           main] o.a.t.restapi.ListVersionHistoryCmd      : Node version class Version {
    id: 2.0
    versionComment: null
    name: somestuff2.txt
    nodeType: acme:document
    isFolder: false
    isFile: true
    modifiedAt: 2021-04-28T12:02:33.526Z
    modifiedByUser: class UserInfo {
        displayName: Administrator
        id: admin
    }
    content: class ContentInfo {
        mimeType: text/plain
        mimeTypeName: Plain Text
        sizeInBytes: 23
        encoding: ISO-8859-1
    }
    aspectNames: null
    properties: null
}
2021-04-29 08:04:48.990  INFO 18326 --- [           main] o.a.t.restapi.ListVersionHistoryCmd      : Node version class Version {
    id: 1.0
    versionComment: null
    name: somestuff2.txt
    nodeType: acme:document
    isFolder: false
    isFile: true
    modifiedAt: 2021-04-28T12:02:33.143Z
    modifiedByUser: class UserInfo {
        displayName: Administrator
        id: admin
    }
    content: class ContentInfo {
        mimeType: text/plain
        mimeTypeName: Plain Text
        sizeInBytes: 0
        encoding: UTF-8
    }
    aspectNames: null
    properties: null
}

Note the id property that contains the file version number. The versionComment property would contain any comments made when uploading a new version of the file. Folder nodes does not have content.