Attachments, change file name before save

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

08 Nov 2017

hello,
is it possible, when attaching a file to an item, change the file name, before the item is saved?

I'm asking because I have the request to make sure all the files uploaded in a Document Library or in a specific List have the same file name structure (i.e. Legal_###.ext), without using the workflow, but with js code on the form.

We are using FD3.1.4, SharePoint 2013 onPremises.

thanks and regards,
Alessandro
ciao,
Ale Stendardo

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

08 Nov 2017

Hello, Alex!
What about using Related Documents instead of attachments? Essentially, it's Related Items with Document Library as a Source and you can configure it in a way which will allow you to set Parent Lookup to the created item, so these files will only be visible for the correct item - https://spform.com/documentation/related-documents

This will also give an advantage of dragging and dropping multiple documents to form at once.

You can also read more about changing uploaded file name here - https://spform.com/javascript-framework ... ctionality

Here's an example code you can use:

Code: Select all

fd.updateDroppedDocuments($('.related-docs'), function(listItem) {
    //new name for the file:
    var prefix = 'Legal_';
    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();
});

fd.updateDroppedDocuments($('.related-docs'), {
  Parent: '{CurrentItem}'
});
Make sure to open form with Related Items in regular mode, not in dialog! It's very important, otherwise this functionality wouldn't work, at least not for the New Form.

Also, please follow instructions from the mentioned articles. You'll need to create Lookup column in Document Library to link documents to specific items, in this example called Parent, and also give certain CSS class to Related Items control, in this example called related-docs

You will not see changes straight away, but once the item is saved, all the changes will apply.
Cheers

User avatar
AlexStenAlexSten
Posts: 31
Joined: Fri Apr 07, 2017

09 Nov 2017

Hi Nikita,
thank your for your reply. It is very useful indeed.
I'm going to try if I can use it for this specific case I have.

I have used Related Documents for other site / list the and I think is a great tool, but I didn't find a way to show also the folders where the files are stored.
Is it a limitation of Related Document, or I'm doing something wrong?

thank you.

Ciao
ciao,
Ale Stendardo

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

09 Nov 2017

Hello, Alex!
This only happens if you configure filtering for the Related Documents. Since in the example you want to only display documents that are related to this specific item, folders are ignored and not displayed.
Cheers

Max Tampa
Posts: 9
Joined: Tue May 16, 2017

10 Nov 2017

Can I use the renaming scrip to get around using same file names in a library?
Currently I have files with similar name - Example " Sales Contract"

Can I use metadata to change the file name on upload?
below is the current script.

fd.updateDroppedDocuments($('.related-docs'), {
  RelatedTestBUTemplate: '{CurrentItem}'
});



fd.updateDroppedDocuments($('.related-docs'), function(listItem) {
//get the values from the Form fields:
var CommunityName = fd.field('ProjectName').value();
var LotNumber = fd.field('Lot').value();
var BuyLastName = fd.field('Buyer1NameLast').value();
var UnitText = fd.field('BusinessUnit').value();
var UnitId = fd.field('BizUnitId').value();
var FormTitle = 'NSA Form Submission';
var DocTypevar = fd.field('DocType').value();
//set the values on uploaded document:
listItem.set_item('Community', CommunityName);
listItem.set_item('Lot', LotNumber);
listItem.set_item('BuyerLastName', BuyLastName);
listItem.set_item('BusinessUnit', UnitText);
listItem.set_item('BusinessUnitId', UnitId);
listItem.set_item('Submittedby', _spPageContextInfo.userId);
listItem.set_item('Title', FormTitle);
listItem.set_item('RelatedBusinessUnit', UnitId);
listItem.set_item('DocumentType', DocTypevar);
listItem.update();
});

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

13 Nov 2017

Hello, Max!
Yes, you can use it to rename file on upload as well, you'll just need to update file twice - first after changing properties and second after renaming:

Code: Select all

fd.updateDroppedDocuments($('.related-docs'), {
    RelatedTestBUTemplate: '{CurrentItem}'
});

fd.updateDroppedDocuments($('.related-docs'), function(listItem) {
    //get the values from the Form fields:
    var CommunityName = fd.field('ProjectName').value();
    var LotNumber = fd.field('Lot').value();
    var BuyLastName = fd.field('Buyer1NameLast').value();
    var UnitText = fd.field('BusinessUnit').value();
    var UnitId = fd.field('BizUnitId').value();
    var FormTitle = 'NSA Form Submission';
    var DocTypevar = fd.field('DocType').value();
    
    //set the values on uploaded document:
    listItem.set_item('Community', CommunityName);
    listItem.set_item('Lot', LotNumber);
    listItem.set_item('BuyerLastName', BuyLastName);
    listItem.set_item('BusinessUnit', UnitText);
    listItem.set_item('BusinessUnitId', UnitId);
    listItem.set_item('Submittedby', _spPageContextInfo.userId);
    listItem.set_item('Title', FormTitle);
    listItem.set_item('RelatedBusinessUnit', UnitId);
    listItem.set_item('DocumentType', DocTypevar);
    
    listItem.update();
    
    //new name for the file:
    var newName = "newName.txt";
 
    //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();
});
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests