Page 1 of 1

Related documents | Extending the original file name

Posted: 30 Oct 2018
by tobib
Hi all,

again I have some challenges that I am unable to solve.

I am using related documents to attach files to a form. That is working fine. But I want to change the name of the document that the user has uploaded.

So that the different name (before uploading and after) does not confuse the user, I want to keep his name and extend it.

Therefore, I want to write the original name in the column "Title" and add the form's ID to the original name in the field "Name". Otherwise I am have a problem if someone wants to attach a file with the same name to form ID 1 and ID 2 as the name is not unique anymore. Adding the form's ID would prevent that.

Example:
User uploads the file "Testfile" within the form ID 1.

After the upload, I want the "Title" to be "Testfile" and the name of the file to be "Testfile-1".

As I couldn't get the filename I used the path and wanted to extract the name.

fd.updateDroppedDocuments($('.attachies'), function(listItem) {

var testvar = listItem.get_file(); //getting the path
var lengthie = testvar.length;
var end = lengthie - 4; //getting rid of the file format
var output = testvar.substring(10,end); //extract the filename without file format and library name
listItem.set_item('Title', output);

listItem.update();
});

As I couldn't even get the file name and fill "Title" I haven't tried yet to change the actual file's name. Shouldn't be such a big problem as I have found one of your instructions to do so. But before editing the file's name I need to know it.

When I am using the above mentioned code the field "Title" is blank after uploading. However, if I try to write the entire path into "Title", it works. So the error has to be "length" or "substring".

Thank you very much in advance for your support!

Re: Related documents | Extending the original file name

Posted: 31 Oct 2018
by AlexZver
Hi!

You can extract the document's name by this code:

Code: Select all

var currentName = listItem.get_fieldValues().FileLeafRef;
If you want to rename the document, please use this code:

Code: Select all

 listItem.get_file().moveTo(newFileName);
These links might be helpful:
https://spform.com/javascript-framework ... ctionality
viewtopic.php?f=1&t=2209

Re: Related documents | Extending the original file name

Posted: 31 Oct 2018
by tobib
Hi,

thanks for the information. I have found that code, too.

But when I am using listItem.get_fieldValues().FileLeafRef there is still the file format left at the end (e. g. ".pdf").

How is it possible to get rid of the file format? So that the name is not "document.docx" but only "document"?

Apparently "substring" can't be used here.

Thank you very much!

Re: Related documents | Extending the original file name

Posted: 31 Oct 2018
by AlexZver
Hmm, ok, let's try this:

Code: Select all

var currentName = listItem.get_fieldValues().FileLeafRef;
var extension = listItem.get_fieldValues().File_x0020_Type;
var currentNameWithoutExtension = currentName.slice (0,-extension.length-1);

Re: Related documents | Extending the original file name

Posted: 31 Oct 2018
by tobib
Works perfectly!

Thanks a lot! Really appreciating your support!