AnsweredAssumed Answered

Webscript and versioning

Question asked by zafarale on May 13, 2014
Latest reply on May 14, 2014 by angelborroy
I have a put webscript which recieved form
Find content with provided id
Updates content
File name
Tags
And saves it by calling node.save

But alfresco is throwing error

org.alfresco.service.cmr.version.VersionServiceException: 04120024 The current version label of the node does not exist in the version history."


Most annoyingly it does that in 70% of cases.
This file resides in space which has versioning aspect enabled

here is the script


var paramsArray = new String(url.extension).split("/")

var homeId = paramsArray[0];
var fileId = paramsArray[2];

var filename = null;
var content = null;
var tagsList;
var title;
var description;

for each (field in formdata.fields) {
   if (field.name == "title") {
      title = field.value;
   }
   else if (field.name == "description") {
      description = field.value;
   }
     else if (field.name == "tagsList") {
       tagsList = field.value;
     }
   
   if (field.name == "file" && field.isFile) {
      filename = field.filename;
      content = field.content;
   }
}


if (homeId.length == 0 || fileId.length == 0) {
   status.code = 400;
   status.message = "A required parameter has not been provided.";
   status.redirect = true;
} else if (filename == undefined || content == undefined) {
      // ensure mandatory file attributes have been located
        status.code = 400;
        status.message = "Uploaded file cannot be located in request.";
        status.redirect = true;
} else {
   var fileExists = false;   
   var file;
   var homesFolder = companyhome.childByNamePath("FOLDER");
   var targetFolder = homesFolder.childByNamePath(homeId);
   var duplicateFileName = false;
   if (targetFolder != undefined) {
      var children = targetFolder.getChildren();
      for (var i = 0; i < children.length; i++) {
         if (new String(children.getId()).valueOf() == new String(fileId).valueOf()) {
            fileExists = true;
            file = children;
         } else if (new String(children.getName()).valueOf() == new String(filename).valueOf()) {
            //File name already belongs to another file.
            duplicateFileName = true;
         }
      }
   }


   if (fileExists && !duplicateFileName) {
      
      if (title != undefined) {
         file.properties.title = title;
      }
      if (description != undefined) {
         file.properties.description = description;
      }

      var fileTags = file.getTags();
      for (var i = 0; i < fileTags.length; i++) {
         if (new String(fileTags).substring(0,4) != "api.") {
            file.removeTag(fileTags);
         }
      }
         file.addTag(tagsList);

      if (filename != undefined && !new String(filename).valueOf() == "") {
         file.properties.name = filename;
         file.properties.content.write(content);
         file.properties.content.setEncoding("UTF-8");
         file.properties.content.guessMimetype(filename);
      }

      file.save();
      model.upload = file;
      model.homeid = homeId;

   } else if (!fileExists) {
      status.code = 400;
      status.message = "File specified (" + fileId + ") does not exist for Id: " + homeId;
      status.redirect = true;
   } else {
      status.code = 400;
      status.message = "A file with this name already exists: " + filename;
      status.redirect = true;
   }

Outcomes