List and Library rename the file on upload

Hi,

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?

Hello @Rinu,

Are you renaming the file when it is uploaded to the library?

Have you followed the instructions from Update properties of uploaded files article?

Hi Mnikitina,

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();
                });
            });
        });
});

Hello @Rinu,

Please try out the 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
                    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();
                            });
                     });
                   }
            });
        });
});

Thank you Mnikitina, This worked as expected.

1 Like