AnsweredAssumed Answered

How to upload a new version of a file by using WebScript?

Question asked by Aleksey Bykov on Jul 9, 2018
Latest reply on Jul 10, 2018 by angelborroy

I have a simple Spring application (front-end application) that loads files into the Alfresco repository. If the file already exists, its new version is not created.


Repository web script is presented below:


public class CustomFileUploader extends DeclarativeWebScript {
     private static final String FIRM_DOC =
     private static final String FIRM_DOC_FOLDER =
     private FirmFile firmFile;
     private FileFolderService fileFolderService;
     private ContentService contentService;

     protected Map<String, Object> executeImpl(WebScriptRequest req,
               Status status) {
          return null;
     private void retrievePostRequestParams(WebScriptRequest req) {
        FormData formData = (FormData) req.parseContent();
        FormData.FormField[] fields = formData.getFields();
        for(FormData.FormField field : fields) {
             String fieldName = field.getName();
             String fieldValue = field.getValue();
                         && field.getIsFile()) {
                  String fileName = field.getFilename();
                  Content fileContent = field.getContent();
                  String fileMimetype = field.getMimetype();
                firmFile = new FirmFile(fileName, fileContent,
                         fileMimetype, FIRM_DOC);
     private void writeContent() {
          try {
               NodeRef parentNodeRef = new NodeRef(FIRM_DOC_FOLDER);
               NodeRef fileNodeRef = createFileNode(parentNodeRef,
               ContentWriter contentWriter = contentService.getWriter(fileNodeRef,
                    ContentModel.PROP_CONTENT, true);
          } catch (Exception e) {
     private NodeRef createFileNode(NodeRef parentNode, String fileName) {
          try {
               QName contentQName = QName.createQName(FIRM_DOC);
               FileInfo fileInfo = fileFolderService.create(parentNode,
                    fileName, contentQName);

               return fileInfo.getNodeRef();
          } catch (Exception e) {

     public FileFolderService getFileFolderService() {
          return fileFolderService;

     public void setFileFolderService(FileFolderService fileFolderService) {
          this.fileFolderService = fileFolderService;

     public ContentService getContentService() {
          return contentService;

     public void setContentService(ContentService contentService) {
          this.contentService = contentService;


How to create a new version of a file with the same name by using Java-backed WebScript?


Does this solution correct?


Check if the file exists by using Lucene search: TYPE:"firm:doc" AND @cm\:name:contract.png; (for example) If exists, increment the property cm:versionLabel and create a new version of Node with all the properties (Actually, need to iterate through all the ResultSet and find NodeRef with max value of cm:versionLabel then increment it and create a new Node). Is there more elegant solution?


I would be very grateful for the information.


Thanks to all.