I am using list and library control to upload files to a document library. All works good except, if I upload a new file and if the file name already exists in the SharePoint document library it will overwrite the existing one.
Is there any way we can add an ID of the lookup column on file upload event?
Thanks for the link, I was able to rename the file names with the below code. But i was having trouble renaming the filename with Id of the files uploaded?.
my intention is to append an ID to the uploaded file name something like below.
Filename_ID.fileExtention
Please see my code below
var listOrLibrary = 'SPDataTable1';
var docLibraryTitle = 'fileUploadListTest';
var docLibraryId = '32398E41-12E4-4E25-8618-670FB69362C7';
var extension;
var filename;
fd.spRendered(function() {
fd.control(listOrLibrary).$on('filesUploaded',
function(itemIds) {
//get document library by Title
var library = pnp.sp.web.lists.getById(docLibraryId);
//go through each uploaded Item Id and set field values
library.getListItemEntityTypeFullName().then(function(entityTypeFullName){
var batch = pnp.sp.web.createBatch();
var currentName;
var uri;
for(var i = 0; i < itemIds.length; i++){
//specify which fields to update and how
library.items.getById(itemIds[i]).inBatch(batch).update({Title: fd.field('Title').value}, "*", entityTypeFullName);
library.items.getById(itemIds[i]).select('FileRef').get().then(function(item){
uri = item.FileRef;
extension = uri.substring(uri.lastIndexOf("."));
filename = uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf("."));
alert(filename);
alert(extension);
});
library.items.getById(itemIds[i]).inBatch(batch).update({FileLeafRef: filename + '_' + extension}, "*", entityTypeFullName);
}
batch.execute().then(function(){
fd.control(listOrLibrary).refresh();
});
});
});
});
var listOrLibrary = 'SPDataTable1';
var docLibraryTitle = 'fileUploadListTest';
var docLibraryId = '32398E41-12E4-4E25-8618-670FB69362C7';
var extension;
var filename;
fd.spRendered(function() {
fd.control(listOrLibrary).$on('filesUploaded',
function(itemIds) {
//get document library by Title
var library = pnp.sp.web.lists.getById(docLibraryId);
//go through each uploaded Item Id and set field values
library.getListItemEntityTypeFullName().then(function(entityTypeFullName){
var batch = pnp.sp.web.createBatch();
var currentName;
var uri;
for(var i = 0; i < itemIds.length; i++){
//specify which fields to update and how
var itemId = itemIds[i];
library.items.getById(itemId).inBatch(batch).update({Title: fd.field('Title').value}, "*", entityTypeFullName);
batch.execute();
library.items.getById(itemId).select('FileRef', 'Id').get().then(function(item){
return item
}).then(function(item){
uri = item.FileRef;
var fileID = item.Id;
filename = uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf(".")) + '_' + fileID;
var nbatch = pnp.sp.web.createBatch();
library.items.getById(fileID).inBatch(nbatch).update({FileLeafRef: filename}, "*", entityTypeFullName)
nbatch.execute().then(function(){
fd.control(listOrLibrary).refresh();
});
});
}
});
});
});
@Margo - thank you for sharing this post with me. I have tried a few variations and am honestly lost.
I created a PA Flow that does the following. If I could accomplish the rename in Plumsail and turn off the flow, that is ideal.
Child Document Library Field Details
OrigName = the original file as uploaded by the user
The PA Flow copies the Name value at upload and writes it to this Single Line text field so we have a "before" name for comparison
Name = SharePoint built-in column
Once the two fields (DxMediaStem and BodySiteStem) have values manually selected by the user AFTER upload, the PA Flow concatenates DxMediaStem+ BodySiteStem+ RAND(1000,9999) and renames the file
DxMediaStem = Choice value manually selected by the user AFTER upload, but still in parent Plumsail form; the user cannot save the parent form until providing a value for this field
BodySiteStem = Choice value manually selected by the user AFTER upload, but still in parent Plumsail form; the user cannot save the parent form until providing a value for this field
Rename Name field on UPLOAD with values from two fields (plus a random string) once they have content.
Capture the original file Name and write to another field for comparison
CODE ATTEMPTED
fd.spRendered(function() {
var listOrLibrary = 'Private';
var docLibraryTitle = 'Private';
var docLibraryId = 'Private';
var extension;
var DxMediaStemV = fd.field('DxMediaStem').value;
var BodySiteStemV = fd.field('BodySiteStem').value;
var filename = "DxMediaStemV" + "BodySiteStemV";
fd.control(listOrLibrary).$on('filesUploaded',
function(itemIds) {
//get document library by Id
var library = pnp.sp.web.lists.getById(docLibraryId);
//get document library by Title
//var library = pnp.sp.web.lists.getByTitle(docLibraryTitle);
//go through each uploaded Item Id and set field values
library.getListItemEntityTypeFullName().then(function(entityTypeFullName){
var batch = pnp.sp.web.createBatch();
var currentName;
var uri;
for(var i = 0; i < itemIds.length; i++){
//specify which fields to update and how
var itemId = itemIds[i];
library.items.getById(itemId).inBatch(batch).update({Title: fd.field('Name').value}, "DxMediaStemV" + "BodySiteStemV", entityTypeFullName);
batch.execute();
library.items.getById(itemId).select('FileRef', 'Id').get().then(function(item){
return item;
}).then(function(item){
uri = item.FileRef;
var fileID = item.Id;
filename = uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf(".")) + '_' + fileID;
var nbatch = pnp.sp.web.createBatch();
library.items.getById(fileID).inBatch(nbatch).update({FileLeafRef: filename}, "DxMediaStemV" + "BodySiteStemV", entityTypeFullName);
nbatch.execute().then(function(){
fd.control(listOrLibrary).refresh();
});
});
}
});
});
});