0

Apologize in advance for the longish post, but I am looking for help with some bizarre SP 2013 behavior.

Files are being uploaded by users to a standard library with versioning turned on. We are trying to enforce file naming conventions on upload by insisting that the filenames begin with a specific identifier.

For example, if the identifier is 101 and the file name is "file1.docx" we want the file to be stored as "101file1.docx" When the file is uploaded, the edit form for required metadata prompts the user for the identifier and checks the filename to see if it follows the required naming convention. If not, we change the filename, effectively renaming the file.

This is implemented with the following PreSaveAction() code:

function PreSaveAction() {
    ...
    $("input[title=['Name']").val(ID+fileName); 
return true; }

The code works fine the first time the doc is uploaded - it accepts 'file1.docx' and saves it as '101file1.docx'. If the user re-uploads the same doc (file1.docx), the expected behavior upon the same save would be for sp to create a new version of '101file1.docx'. Instead, it returns an error 'There is already a file with that name, pick another'. Clicking on OK sends the user back to the metadata edit screen, but the doc 'file1.docx' is now uploaded!

It looks like the file is physically uploaded into the server before the save action code runs and that sp behavior cannot be changed. But if so, why does it not happen the first time and is there a solution to rename a file and create a new version?

1 Answer 1

0

In your PreSaveAction(), you always return true. Returning false should cancel the save action.

1
  • I don't want to cancel the save action - I want to save it as a new version. Mar 10, 2017 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.