Related Documents - New Functionality

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

26 Sep 2017

I am looking to dynamically rename documents on upload using the dropped documents function, my idea is to simply prefix the existing name with the current Unix timestamp in seconds, this way I can almost guarantee a unique name; so I was think something along the lines of: -

Code: Select all

fd.updateDroppedDocuments($('.emailUpload), function(listItem) {
    //new name for the file:
    var prefix = Date.now() / 1000 | 0;
    var currentName = [i]Unsure of the code to get the current name?[/i]
    var newName = prefix + currentName;
 
    //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
 
    //finally, renaming the file after all the code:
    listItem.get_file().moveTo(newFileName);
});
Any help with the code would be very much appreciated.

Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

26 Sep 2017

This should work:

Code: Select all

fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
    //new name for the file:
    var prefix = Date.now() / 1000 | 0;
    var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = prefix + currentName;
 
    //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
 
    //finally, renaming the file after all the code:
    listItem.get_file().moveTo(newFileName);
});
Don't forget the second quote mark in $('.emailUpload')
Cheers

User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

28 Sep 2017

Hi Nikita,

Ok so this works, the document is uploaded all the fields I want setting get set and the document is renamed.
One little thing on the document being renamed it disappears from the related items viewer, on save the Linking Items dialogue still appears and on checking the document library it is there, renamed and all correctly linked to the parent, it shows up in the display and edit forms.
This isn't a huge issue, but from a user point of view a new or inexperienced user could think the function had failed and upload multiple copies of the same document.
Is there any way around this?

Thanks again for all your help.

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

28 Sep 2017

So it seems that after renaming the document, it does disappear if you have filtering set up to show only new items. So it shouldn't be a problem for Edit Form. But there is a solution for New Form as well. If you place listItem.update(); after renaming the file, it will still display the file, but it won't show the new name until you refresh the page. So, the file will get renamed, it will get stored with the new name, but you won't see it straight away.

So, your final code will look like this:

Code: Select all

fd.updateDroppedDocuments($('.emailUpload'), function(listItem) {
    //new name for the file:
    var prefix = Date.now() / 1000 | 0;
    var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = prefix + currentName;
 
    //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
 
    //finally, renaming the file after all the code:
    listItem.get_file().moveTo(newFileName);
    listItem.update();
});
Hope this answer helps!
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 4 guests