Page 1 of 1
Attachments, change file name before save
Posted: 08 Nov 2017
by AlexStenAlexSten
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
Re: Attachments, change file name before save
Posted: 08 Nov 2017
by Nikita Kurguzov
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.
Re: Attachments, change file name before save
Posted: 09 Nov 2017
by AlexStenAlexSten
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
Re: Attachments, change file name before save
Posted: 09 Nov 2017
by Nikita Kurguzov
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.
Re: Attachments, change file name before save
Posted: 10 Nov 2017
by Max Tampa
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();
});
Re: Attachments, change file name before save
Posted: 13 Nov 2017
by Nikita Kurguzov
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();
});